[英]How can I allow users of my website to upload media files?
I'm currently following a tutorial for a basic media file upload form. 我目前正在关注基本媒体文件上传表单的教程。 I've tried adding it to my website, but I always get the message "There was an error uploading the file, please try again!"
我尝试将其添加到我的网站,但始终收到消息“上传文件时出错,请重试!” when I try to upload images using it.
当我尝试使用它上传图像时。
I've put the following html file onto my server: 我将以下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>
I have then created the file uploader.php, which looks like this: 然后,我创建了文件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!";
}
?>
I've stored these files in the same directory. 我已将这些文件存储在同一目录中。 I've then created another directory called uploads - which is writable.
然后,我创建了另一个名为uploads的目录-这是可写的。
Why is this not working? 为什么这不起作用?
EDIT: I'm now able to upload files upto 1MB in size, but anything larger than this fails to upload. 编辑:我现在能够上传最大1MB的文件,但是任何大于此的文件都无法上传。 As you can see in the HTML script, I've set the max file size to 10MB - so this should work.
正如您在HTML脚本中看到的那样,我将最大文件大小设置为10MB-这样就可以了。 Any idea why it isn't?
知道为什么不是吗?
It will Work. 它会工作。 I checked in my desktop.
我检查了桌面。
Change value="100000"
to value="1000000"
将
value="100000"
更改为value="1000000"
Like 喜欢
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
And, 和,
$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.
MAX_FILE_SIZE隐藏字段(以字节为单位)必须在文件输入字段之前,其值是PHP接受的最大文件大小。
For more info, click File Upload - php manual 有关更多信息,请单击文件上传-PHP手册
User's Requirement (As, He asked in comment) 用户要求 (如,他在评论中要求)
Append your file name with time()
. 在文件名后加上
time()
。 There are many ways to do. 有很多方法可以做。 But, this one you can use.
但是,这个你可以使用。
$target_path = $target_path .time() .basename( $_FILES['uploadedfile']['name']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.