【问题标题】:HTML form for PHP Image Upload ScriptPHP 图片上传脚本的 HTML 表单
【发布时间】:2015-02-22 20:02:31
【问题描述】:

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

这里是 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.';
}
}

【问题讨论】:

标签: php file-upload


【解决方案1】:

不久前我遇到了这个确切的代码 给你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>

【讨论】:

    【解决方案2】:
    <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>
    

    为您工作的最简单的形式

    【讨论】:

      【解决方案3】:

      你可以添加

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

      在文件输入字段之前。此表单元素设置文件输入字段的最大文件大小,以字节为单位。此 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"/>
      

      【讨论】:

        【解决方案4】:

        在下面保存为 index.php 并在同一目录中创建一个名为 images 的文件夹。请记住在服务器上将图像文件夹更改为 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>';
        }
        
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-15
          • 1970-01-01
          • 2013-07-22
          • 1970-01-01
          • 2011-01-03
          • 2012-07-01
          • 1970-01-01
          相关资源
          最近更新 更多