简体   繁体   English

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

[英]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.

相关问题 如何允许用户将文件上传到网站以便我可以接收它们? - How to allow users to upload files to the website so that I can receive them? 如何轻松允许用户上传多个文件? - How can I easily allow users to upload multiple files? 如何允许 iOS 用户通过我网站上的 html 输入字段上传文档? - How do I allow iOS users to upload a document via an html input field on my website? Web开发:如何允许用户将文件直接上传到我的CDN(Cachefly)? - Web Development: How can I allow a user to upload files directly to my CDN (Cachefly)? 当我的网站有 3 种语言时,如何将文件上传到一个文件夹中? - How can I upload files in one folder when I have 3 language in my website? 如何安全地允许Web用户创建文件? - How can I securely allow web users to create files? 如何显示已上传到网站服务器上文件夹中的文件,以便用户下载? - How can I display the files I have uploaded to a folder in my server on my website so that users can download them? 我可以在网站上安装什么快速,批量上传媒体管理系统? - What is a fast, bulk upload, media management system I can install on my website? 无法上传媒体,我也无法在WordPress网站中看到网格视图吗? - Cannot Upload Media, neither can I see the grid view in my WordPress website? 我可以让我的用户从他们的Instagram帐户中选择一张照片上传到我的网站吗? - Can I have my users select a photo from their Instagram account to upload to my website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM