PHP文件上传在Windows上有效,但在Linux上无效。

huangapple go评论68阅读模式
英文:

PHP file upload works on Windows but not Linux

问题

I am making a website that takes an audio file that is uploaded from a user, mixes it on my server and downloads it back to the users device, but it won't upload the file anymore. Initially I was running the website on my windows server but it for some reason struggled with getting the SSL certificate, so I had to build it from the ground up on Linux RHE. To simplify the issue, I got a bare-bone file uploading script which also worked on my Windows port, but not on this server. The only response I got was "Error moving the uploaded file!", meaning something to do with the /tmp directory I assume and not being able to move it from there? I did look in the directory and it was pretty much empty, not the uploaded files. Here is the code.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Check if the file was uploaded without errors
    if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
        // Get the temporary filename
        $tempFile = $_FILES['file']['tmp_name'];
        $targetDir = '/var/www/html/';
        $targetFile = $targetDir . basename($_FILES['file']['name']);

        if (move_uploaded_file($tempFile, $targetFile)) {
            echo 'File uploaded and moved successfully.'; //I get this error
        } else {
            echo 'Error moving the uploaded file.';
        }
    } else {
        echo 'Error uploading file. Error code: ' . $_FILES['file']['error'];
    }
}
?>

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit">Upload</button>
</form>

I tested the same code on windows (with different directories of course) and it worked. I changed the permissions in the /tmp directory, chown, chmod.. I have made it print errors with $_FILES['file']['error'] which returned "0", meaning no error.

The expected outcome should be a "file uploaded successfully" and the file uploads to the websites directory for the other code to mix it.

英文:

I am making a website that takes an audio file that is uploaded from a user, mixes it on my server and downloads it back to the users device, but it won't upload the file anymore. Initially I was running the website on my windows server but it for some reason struggled with getting the SSL certificate, so I had to build it from the ground up on Linux RHE.
To simplify the issue, I got a bare-bone file uploading script which also worked on my Windows port, but not on this server. The only response I got was "Error moving the uploaded file!", meaning something to do with the /tmp directory I assume and not being able to move it from there? I did look in the directory and it was pretty much empty, not the uploaded files. Here is the code.

`&lt;?php
if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
    // Check if the file was uploaded without errors
    if (isset($_FILES[&#39;file&#39;]) &amp;&amp; $_FILES[&#39;file&#39;][&#39;error&#39;] === UPLOAD_ERR_OK) {
        // Get the temporary filename
        $tempFile = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];
        $targetDir = &#39;/var/www/html/&#39;;
        $targetFile = $targetDir . basename($_FILES[&#39;file&#39;][&#39;name&#39;]);
    
        if (move_uploaded_file($tempFile, $targetFile)) {
            echo &#39;File uploaded and moved successfully.&#39;; //I get this error
        } else {
            echo &#39;Error moving the uploaded file.&#39;;
        }
    } else {
        echo &#39;Error uploading file. Error code: &#39; . $_FILES[&#39;file&#39;][&#39;error&#39;];
    }
}
?&gt;

&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;file&quot;&gt;
    &lt;button type=&quot;submit&quot;&gt;Upload&lt;/button&gt;
&lt;/form&gt;`

I tested the same code on windows (with different directories of course) and it worked. I changed the permissions in the /tmp directory, chown, chmod.. I have made it print errors with $_FILES['file']['error'] which returned "0", meaning no error.

The expected outcome should be a "file uploaded successfully" and the file uploads to the websites directory for the other code to mix it.

答案1

得分: 1

有可能问题出在目标路径,我们必须修改/var/www/html/的权限。

另一个可能的问题是你正在使用原始文件名,如果已经有同名文件,可能会导致错误。

英文:

It is possible that the problem is in the destination path, we must modify the permissions of
/var/www/html/ too

Another possible problem is that you are using the original file name, which can cause an error if there is already a file with the same name.

huangapple
  • 本文由 发表于 2023年5月13日 09:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定