Html 上传图片的HTML代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9692454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:06:30  来源:igfitidea点击:

HTML code to upload images

html

提问by Garry

Can someone please provide me with some links (or source code) for me to research about how to upload images to a website (by the community) and for these images to be viewed online by the community.

有人可以为我提供一些链接(或源代码),让我研究如何将图像上传到网站(由社区)以及如何让社区在线查看这些图像。

I have searched via google, but have had no luck.

我已经通过谷歌搜索过,但没有运气。

Thank you.

谢谢你。

EDIT

编辑

Sorry, I have actually seen many examples to upload files. I am wanting (if possible) to be able to upload images, and then have them arranged in some kind of gallery so that the community can see/view them.

抱歉,我其实看过很多上传文件的例子。我希望(如果可能)能够上传图像,然后将它们安排在某种画廊中,以便社区可以查看/查看它们。

I am after a range of different technologies that are available on the WWW if possible.

如果可能的话,我正在寻找 WWW 上可用的一系列不同技术。

回答by Venkat Selvan

Copy the below codes and save it as upload.php or anything.php (note it should be saved as php extension and should be run on apache server or any server supporting php).

复制以下代码并将其保存为upload.php 或anything.php(注意它应该保存为php 扩展名,并且应该在apache 服务器或任何支持php 的服务器上运行)。

<?php
if(isset($_REQUEST['submit']))
{
  $filename=  $_FILES["imgfile"]["name"];
  if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] == "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png")  || ($_FILES["imgfile"]["type"] == "image/pjpeg")) && ($_FILES["imgfile"]["size"] < 200000))
  {
    if(file_exists($_FILES["imgfile"]["name"]))
    {
      echo "File name exists.";
    }
    else
    {
      move_uploaded_file($_FILES["imgfile"]["tmp_name"],"uploads/$filename");
      echo "Upload Successful . <a href='uploads/$filename'>Click here</a> to view the uploaded image";
    }
  }
  else
  {
    echo "invalid file.";
  }
}
else
{
?>
<form method="post" enctype="multipart/form-data">
File name:<input type="file" name="imgfile"><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
}
?> 

And you should create two folders("uploads" and "tmp") in the parent directory (Where the script executes).

并且您应该在父目录(脚本执行的位置)中创建两个文件夹(“uploads”和“tmp”)。

Now your upload script is ready. It's simply an upload script with which you can upload one image at a time.

现在您的上传脚本已准备就绪。它只是一个上传脚本,您可以使用它一次上传一张图片。

回答by Joe Meyer

In order to get a list of files in a directory (assumes the file path is known ahead of time) you can do something like this:

为了获取目录中的文件列表(假设文件路径提前已知),您可以执行以下操作:

$filePath =  $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);

If you wanted to filter this down to look for image files you could use something like this:

如果您想过滤它以查找图像文件,您可以使用以下内容:

$filePath =  $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'uploads';
$files = scandir($filePath);

$imageFiles = array_values(preg_grep('/^.*?\.((gif)|(png)|(jpe?g))$/', $files));

And for a good guide on uploading the files you can check out http://www.exchangecore.com/blog/how-upload-files-html-php/

有关上传文件的良好指南,您可以查看http://www.exchangecore.com/blog/how-upload-files-html-php/

回答by Brian

Here's a good, basic start using PHP: http://www.w3schools.com/php/php_file_upload.asp

这是使用 PHP 的一个很好的基本开始:http: //www.w3schools.com/php/php_file_upload.asp