我想知道如何在上传文件时彻底更改名称。 我有一个文本输入“名称”,所以我试图把$ _POST ['name']放到moveuploadedfile函数,但没有结果。我应该把它放在哪里?
html
<form id="upload" enctype="multipart/form-data" action="upload.php"method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="watermark" placeholder="watermakr" />
<input type="file" name="file" /><br />
<input type="submit" value="send" />
</form>
PHP
$uploads_dir ='/usr/local/apache/htdocs/images/';
$target_file = $uploads_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Only JPG, JPEG, PNG files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "ERROR";
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "File". basename( $_FILES["file"]["name"]). " was sent.";
答案 0 :(得分:1)
旧行:
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)
新行:
move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $_POST['name'])
答案 1 :(得分:-1)
而不是
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "File". basename( $_FILES["file"]["name"]). " was sent.";
使用
$ext = last(explode('.', $_FILES['file']['tmp_name']));
$custom_name = 'something'; // or $_POST['name']
$imageName = $custom_name.'.'.$ext;
move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $imageName);