PHP:: 文件上传成功(但未上传)

PHP:: File successfully uploaded (but not)

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

这是我的代码:

<?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>

这里需要做一些小调整:)

你需要把它分开:

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/,而不是 $uploadedFile 的值。所以这实际上会被评估为 gangina/1 之类的东西,因为 $uploadedFile 的声明成功,给出 true,被评估为 1.

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

也可以看看 this example code on php.net。这显示了如何执行所有必要的检查,以便在 PHP.

中处理文件上传时向用户提供更准确的反馈。

为您的提交按钮命名,例如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);

  }

?>