繁体   English   中英

PHP -- move_uploaded_file 不起作用

[英]Php -- move_uploaded_file not working

抱歉,我知道有很多关于此的帖子,但我无法在其中找到解决方案。

这是我的表格:

<form id="form1" action="upload.php" method="post" enctype="multipart/form-data">
<table>
    <tr>
        <td>Name : </td>
        <td><input type="text" id="name" name="name"/></td>
    </tr>
    <tr>
        <td>Image :</td>
        <td><input type="file" name="image"/></td>
    </tr>
    <tr><td id='submitAdd' colspan='2'><input type="submit" 
    value= "  Add  " /></td></tr>
  </table>
</form>

这里是 upload.php :

<?php
    $ext = strtolower(substr(strrchr($_FILES['image']['name'], '.'),1));
    $ret = move_uploaded_file($_FILES['image']['tmp_name'], 'item_images/'.$_POST['name'].'.'.$ext);
    if ($ret) {
        echo 'works';
    }
    else {
        echo 'doesnt work'."</br>";
        echo $_FILES['image']['error'];
    }
?>

目录的权限没问题,没有上传错误,但它仍然不会移动文件。 我错过了什么吗?

提前致谢

我认为您需要指定绝对保存路径,而不是您现在拥有的相对路径。

前任。 dirname(__FILE__).'/item_images/'.$_POST['name'].'.'.$ext

我会在上传的文件名下移动文件,然后重命名它。 此外,一些文件类型检查和安全应该添加到这个.. 消毒后等.. 这是我将如何做到这一点。

上传.php

<?php 
$fileName = $_FILES["image"]["name"]; // The file name
$fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["image"]["type"]; // The type of file it is
$fileSize = $_FILES["image"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["image"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$fname = $kaboom[0];
$exten = strtolower($fileExt);

//now we do some security checks

if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than xxx Megabytes in size.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
 // This condition is only if you wish to allow uploading of specific file types    
 echo "ERROR: Your image was not .gif, .jpg, or .png.";
 unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
 exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}

//give it the new name
$userstring= $_POST['name'];   
$string = $fname.$userstring.'.'.$exten;
$image_name = preg_replace('/\s+/', '', $string);

//now we move it. 

$moveResult = move_uploaded_file($fileTmpLoc, "item_images/$image_name");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
echo "ERROR: File not uploaded. Try again.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
}
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder   
$imageFile = "item_images/$image_name";  
//$imageFile is the variable to use in the rest of your script.

?>

我只是如何解决这个问题。 这对我有用,您可以尝试一下:只需更改

item_images/'.$_POST['name'].'.'.$ext);

'item_images/'basename($_FILES["image"]["name"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM