我有一个"奇怪的"有关PHP中move_uploaded_file的问题。
我在stackoverflow上阅读了所有问题并回答了我的问题,但没有一个能解答我的问题。
奇怪的是:如果从未在tmp文件夹中创建临时文件,那就好了,所以当我调用move_uploaded_file时,它就是"窃听"的行。 我想说在我的代码中我浏览了所有" IF"子句和变量$ uploadOk告诉我一切正常,我可以上传我的文件。
这里是代码: 以我的形式:
<form action="enregistrement.php?act=<?PHP echo $methode ?>" method="post" enctype="multipart/form-data">
<button type='submit' class='btn-post'>Submit</button>
</form>
如您所见,方法(POST)和enctype(multipart / form-data)都很好。
在&#34; enregistrement.php&#34;页面:
$uploadOk = 1;
$target_file = "";
function SaveAvatar(&$uploadOk, &$target_file){
$target_dir = "/assets/uploads/";
$target_file = $target_dir . basename($_FILES["userAvatar"]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["userAvatar"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
//echo "Sorry, file already exists.";
//$uploadOk = 0;
}
// Check file size
if ($_FILES["userAvatar"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
echo($_FILES["userAvatar"]["tmp_name"]);
echo( "::" );
echo($target_file);
exit;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["userAvatar"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["userAvatar"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
**第一:我尝试了一些ECHO来验证变量。
唯一的事情就是当我运行程序时我从未看到我在我的tmp目录中打开文件浏览器,直到程序停止使用EXIT,我从未在此目录中看到上传文件,只是请参阅会话文件。
你们其中一个人是否看到了我错过的东西? 感谢