简体   繁体   English

文件上传在 Apache 服务器上的 PHP 中不起作用

[英]File upload does not work in PHP on Apache Server

I'm trying to upload an image file onto a Web server running on a Raspberry.我正在尝试将图像文件上传到在 Raspberry 上运行的 Web 服务器上。 The image should be uploaded through a form and forwarded to a PHP script.图像应通过表单上传并转发到 PHP 脚本。 I checked if the folders are set to the proper permissions and it seems OK.我检查了文件夹是否设置为正确的权限,看起来没问题。 Code seems to run without a problem.代码似乎运行没有问题。

Here is the form:这是表格:

<form action="./php/fileUpload.php" method="POST" enctype="multipart/form-data"> 
   Wähle eine Datei aus: <br> <input name="datei" type="file" />
   <input type="submit" value="Sende Datei" />
</form>

The file selected in this form will be sent to this script:在此表单中选择的文件将发送到此脚本:

<?php
$upload_dir = "/uploads/";
$file_extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));
$file_filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);
$targeted_Upload = $upload_dir . $file_filename . "." . $file_extension;
$temp_file = $_FILES["datei"]["tmp_name"];

var_dump($upload_dir);
echo "<br>";
var_dump($targeted_Upload);
echo "<br>";
var_dump($file_filename);
echo "<br>";
var_dump($file_extension);
echo "<br>";
var_dump($temp_file);
echo "<br>";

if(file_exists($targeted_Upload)){
    echo "file exists... renaming";
    echo "<br>";
    $newName = $upload_dir . $file_filename;
    do{
        $newName = $newName . "_1";
    }while(file_exists(($newName . "." . $file_extension)));

    $targeted_Upload = $newName . "." . $file_extension;

    echo "file was renamed to " . $targeted_Upload;
    echo "<br>";
    var_dump($targeted_Upload);
    echo "<br>";
}

if ($_FILES["datei"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
}

echo"$targeted_Upload";
echo "<br>";

if(move_uploaded_file($temp_file, $targeted_Upload)){
    echo "<br>File was Uploaded" . "<a href=\"$targeted_Upload\">$targeted_Upload</a>";
}else{
    echo "I have no clue what happened";
}
?>

When I execute the script the output on the page is as following当我执行脚本时,页面上的输出如下

string(9) "/uploads/"
string(16) "/uploads/ken.jpg"
string(3) "ken"
string(3) "jpg"
string(14) "/tmp/phpv0JAu1"
/uploads/ken.jpg
I have no clue what happened

Does anyone have a clue what I did wrong or what went wrong?有谁知道我做错了什么或出了什么问题?

I think there is problem with your upload path.我认为您的上传路径有问题。 if PHP script and uploads is in the same directrory then you should use dot[.] for current directory如果 PHP 脚本和上传在同一个目录中,那么你应该使用 dot[.] 作为当前目录

$upload_dir = "./uploads/"; 

You can check upload error.您可以检查上传错误。

 if(move_uploaded_file($temp_file, $targeted_Upload)){
    echo "<br>File was Uploaded" . "<a href=\"$targeted_Upload\">$targeted_Upload</a>";
}else{
    echo "Not uploaded because of error #".$_FILES["datei"]["error"];
}

UPLOAD_ERR_INI_SIZE = Value: 1; UPLOAD_ERR_INI_SIZE = 值:1; The uploaded file exceeds the upload_max_filesize directive in php.ini.上传的文件超过了 php.ini 中的 upload_max_filesize 指令。

UPLOAD_ERR_FORM_SIZE = Value: 2; UPLOAD_ERR_FORM_SIZE = 值:2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.上传的文件超过了在 HTML 表单中指定的 MAX_FILE_SIZE 指令。

UPLOAD_ERR_PARTIAL = Value: 3; UPLOAD_ERR_PARTIAL = 值:3; The uploaded file was only partially uploaded.上传的文件只是部分上传。

UPLOAD_ERR_NO_FILE = Value: 4; UPLOAD_ERR_NO_FILE = 值:4; No file was uploaded.没有上传文件。

UPLOAD_ERR_NO_TMP_DIR = Value: 6; UPLOAD_ERR_NO_TMP_DIR = 值:6; Missing a temporary folder.缺少临时文件夹。 Introduced in PHP 5.0.3.在 PHP 5.0.3 中引入。

UPLOAD_ERR_CANT_WRITE = Value: 7; UPLOAD_ERR_CANT_WRITE = 值:7; Failed to write file to disk.无法将文件写入磁盘。 Introduced in PHP 5.1.0.在 PHP 5.1.0 中引入。

UPLOAD_ERR_EXTENSION = Value: 8; UPLOAD_ERR_EXTENSION = 值:8; A PHP extension stopped the file upload. PHP 扩展停止了文件上传。 PHP does not provide a way to ascertain which extension caused the file upload to stop; PHP 没有提供确定哪个扩展名导致文件上传停止的方法; examining the list of loaded extensions with phpinfo() may help.使用 phpinfo() 检查加载的扩展列表可能会有所帮助。

You need to do two things:你需要做两件事:

  1. Use the absolute file path in the destination parameter in move_uploaded_file() .move_uploaded_file()的目标参数中使用绝对文件路径。

    Edit move_uploaded_file and append the destination parameter with __DIR__.'/'.编辑move_uploaded_file并使用__DIR__.'/'.附加目标参数__DIR__.'/'. : if(move_uploaded_file($temp_file, __DIR__.'/'.$targeted_Upload)){ ... } : if(move_uploaded_file($temp_file, __DIR__.'/'.$targeted_Upload)){ ... }

  2. Make sure that the directory /uploads is created, and that the web server user have permissions to write to it.确保目录/uploads已创建,并且 Web 服务器用户具有写入权限。

File will be stored in temporary location- Give full path of the upload location.文件将存储在临时位置 - 提供上传位置的完整路径。

if (move_uploaded_file($temp_file, $_SERVER['DOCUMENT_ROOT']/project_name/$targeted_Upload)) {
    echo "Uploaded";
} else {
   echo "File was not uploaded";
}

I had permission issue to the target folder(In case the path is correct).If anyone by chance having this issue, please give permission to the folder using the below command in the terminal.我对目标文件夹有权限问题(如果路径正确)。如果有人偶然遇到此问题,请在终端中使用以下命令授予文件夹权限。

sudo chmod 777 /path/to/upload/folder

Please note I am using Ubuntu 18.04LTS.请注意我使用的是 Ubuntu 18.04LTS。

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

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