英文:
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.
`<?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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论