-3

我正在尝试通过以下代码上传图片。尽管我的 if 条件正在执行,如下所示,但图像没有移动到我给出的指定路径。我的代码如下。这是我的html代码

<form enctype="multipart/form-data" action="catcher.php" method="POST">
Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

这是我的php代码

<?php
$uploaddir = '/xampp/project/';
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

任何人都可以指出我的代码有什么问题吗?我的意思是为什么我要上传的图像没有移动到指定的路径,,

4

2 回答 2

2

move_uploaded_file()要求第二个参数是目标文件,而不是目录。

你可以这样做:

$filename = $uploaddir . basename($_FILES['userfile']['tmp_name']);

if( move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)){
// ...

但是很有可能多个用户会尝试上传同名文件,所以我强烈建议处理这种可能性。

于 2013-04-23T08:28:55.213 回答
0
 $folder =  "../images/stories/";
$result =   move_uploaded_file($_FILES['excel']['tmp_name'], $folder.$filename);

给出目标文件夹名称。(在上面的代码中,$folder 是目标路径变量)您可以查看此函数的详细信息:

function upload($name,$tmp_name){   

$fileextarr=explode('.',$name);
$fileextarr[0];
$fileext=$fileextarr[count($fileextarr)-1];
$fpath = "../images/stories/lab_excel/";
$filename = ereg_replace(" ","_",$fileextarr[0]).'.'.$fileext;


//You can use restriction on file extensions also. (not necessary)

if($fileext=="xls" || $fileext=="xlsx" || $fileext=="doc"|| $fileext=="docx" || $fileext=="jpeg" || $fileext=="png" || $fileext=="gif" || $fileext=="tiff" || $fileext=="bmp" || $fileext=="jpg")
{
    $uploadedfile = $filename;

}

//Here is the use of destination path:



 $folder =  "../images/stories/lab_excel/";
$result =   move_uploaded_file($_FILES['excel']['tmp_name'], $folder.$filename);

/*echo $folder.$_FILES['excel']['name'];
die();*/

if($result){
$msg="File Uploaded Successfully";
    return $folder.$filename;
}
else
{
    return false;
}

}

于 2013-04-23T08:38:38.070 回答