PHP ::文件已成功上传(但不是)

时间:2015-02-01 12:23:29

标签: php forms file post upload

Wierd ......我知道该文件已成功上传但在现实世界中......没有上传任何内容...

这是我的代码:

<?php

if
(
    move_uploaded_file
    (
        $_FILES['myUploadedFile']['tmp_name'],
        'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name'])
    )
)
{
    echo "The file ".$uploadedFile." has been uploaded";
}
else
{
    echo "There was an error uploading the file, please try again!";
}

?><!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>

<form action="#" method="POST" enctype="multipart/form-data">
<input name="myUploadedFile" type="file">
<input type="submit" value="Upload">
</form>

</body>
</html>

此处需要进行小调整:)

2 个答案:

答案 0 :(得分:2)

您需要将其拆分:

if (move_uploaded_file
(
    $_FILES['myUploadedFile']['tmp_name'],
    'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name'])
)) {
// ...

相反,请执行:

$uploadedFile = basename($_FILES['myUploadedFile']['name']);
if (move_uploaded_file
(
    $_FILES['myUploadedFile']['tmp_name'],
    'gangina/' . $uploadedFile
)) {
// ...

当您执行'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name'])时,您实际上将 $uploadedFile声明附加到gangina/,而不是 {/ em> { {1}}。所以这实际上会被评估为像$uploadedFile这样的东西,因为gangina/1的声明成功,给出了真,它被评估为1。

您还需要检查网络服务器(通常是Linux上的用户www-data)是否有权在$uploadedFile文件夹中创建新文件(并且该文件夹实际存在)。在Windows下,这通常不是问题,除非您在Program Files下安装了Xampp。

另请查看this example code on php.net。这显示了在PHP中处理文件上载时如何执行所有必要的检查以向用户提供更精确的反馈。

答案 1 :(得分:1)

为您的提交按钮添加一个名称,例如name="uploadImage"

 <?php 
function uploadImage($image,$ftp_file){
// Path and file name
$imgUrl = "gangina/".$image;
if (file_exists($imgUrl)){
$temp = str_ireplace('gangina/', '', $image);
$imgUrl = "gangina/". rand(1,99999).$temp;
}
$img = str_ireplace('gangina/', ' ', $imgUrl);
 // Run the move_uploaded_file() function here
if(move_uploaded_file($ftp_file, $imgUrl)){
$results = "image successfully uploaded";
   }  else {
   $results = 'Could not upload image'; 
    }
  return $results;
}
 if(isset($_POST['uploadImage']{
    $imgurl = $_FILES['ImageName']['name'];
    $temp = $_FILES['ImageName']['tmp_name'];
    //uploading image
    uploadImage($imgurl, $temp);

  }

?>