PHP 图片上传脚本的 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20172353/
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-25 02:57:00  来源:igfitidea点击:

HTML form for PHP Image Upload Script

phpfile-upload

提问by user3026799

Can anyone give me the html code for this php image upload script. I really need it please if anyone can help me on this I will be grateful to you.

谁能给我这个 php 图片上传脚本的 html 代码。我真的需要它,如果有人可以帮助我,我将不胜感激。

Here is php code:

这是php代码:

if(isset($_POST['upload'])) {

$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';
$description = $_POST['imgdesc'];

$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');

if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
   $query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)"; 
   mysql_query($query);

echo 'Your file upload was successful!';


} else {
     echo 'There was an error during the file upload.  Please try again.';
}
}

回答by Mario Segura

I came across this exact code a while back Here you go for html

不久前我遇到了这个确切的代码,您可以使用 html

<form action="/script.php" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile"/>
    <input type="text" name="imgdec">
    <button name="upload" type="submit" value="Submit">
</form>

回答by Satya

<form name="myFrm" id="myFrm" action="uraction" method="post" enctype="multipart/form-data" >
<label for="upload" >Select  Image</label><input type="file" id="upload" name="upload" accept="image/*">
<p/>
<input type="submit" value="Go" >
</form>

Bare minimum form to work for you

为您工作的最低要求

回答by Hasib Mahmud

You can add

你可以加

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

before the file input field. This form element set the maximum file size of the file input field and it is measured in bytes. This MAX_FILE_SIZE is applied to the file inputs that come after it. Remember, this does not indicate the total size of all the input files. See the following example:

在文件输入字段之前。此表单元素设置文件输入字段的最大文件大小,以字节为单位。此 MAX_FILE_SIZE 应用于其后的文件输入。请记住,这并不表示所有输入文件的总大小。请参阅以下示例:

<input type="hidden" name="MAX_FILE_SIZE" value="10000"/>
<!--for these two consecutive input fields, maximum file size is 10000 bytes -->
<input type="file" name="userfile1"/>
<input type="file" name="userfile2"/>
<input type="hidden" name="MAX_FILE_SIZE" value="50000"/>
<!--for this input field, maximum file size is 50000 bytes -->
<input type="file" name="userfile3"/>

回答by cardazio

Save below as index.php and create a folder in the same directory called images. Remember to chmod the images folder to 777 once on the server.

将下面保存为 index.php 并在同一目录中创建一个名为 images 的文件夹。请记住在服务器上将图像文件夹 chmod 为 777 一次。

<?php


if(isset($_GET['do']) && $_GET['do'] == 'upload2') {

// Start


$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'images/';


$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');

if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
//   $query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)"; 
//   mysql_query($query);

echo 'Your file upload was successful!';


} else {
     echo 'There was an error during the file upload.  Please try again.';
}


// Finish


} elseif(isset($_GET['do']) && $_GET['do'] == 'upload1') {
echo '

<form action="index.php?do=upload2" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile"/>

    <button name="upload" type="submit" value="Submit">
</form>


';
} else {
echo '<a href="index.php?do=upload1">Link</a>';
}


?>