如何允许我网站的用户上传媒体文件?

How can I allow users of my website to upload media files?

我目前正在学习基本媒体文件上传表单的教程。我试过将它添加到我的网站,但当我尝试使用它上传图片时,我总是收到消息 "There was an error uploading the file, please try again!"。

我已将以下 html 文件放到我的服务器上:

<DOCTYPE! html>
<html>


<head>
</head>


<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>


</html>

然后我创建了文件 uploader.php,它看起来像这样:

<?php

// Where the file is going to be placed 
$target_path = "../uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 



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

?>

我已将这些文件存储在同一目录中。然后我创建了另一个名为 uploads 的目录 - 这是可写的。

为什么这不起作用?

编辑:我现在可以上传最大 1MB 的文件,但超过此大小的文件将无法上传。正如您在 HTML 脚本中看到的那样,我已将最大文件大小设置为 10MB - 所以这应该可行。知道为什么不是吗?

它会起作用。我检查了我的桌面。

value="100000"更改为value="1000000"

喜欢

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

并且,

$target_path = "uploads/";

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP.

有关详细信息,请单击 File Upload - php manual

用户需求(如,何评论)

在您的文件名后附加 time()。有很多方法可以做到。但是,这个你可以用。

$target_path = $target_path .time() .basename( $_FILES['uploadedfile']['name']);