PHP图像上载脚本的HTML表单

时间:2013-11-24 07:51:25

标签: php file-upload

任何人都可以给我这个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.';
}
}

4 个答案:

答案 0 :(得分:11)

我前一段时间遇到了这个确切的代码 在这里你去找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>

答案 1 :(得分:3)

<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>

为您工作的裸体最低形式

答案 2 :(得分:2)

您可以添加

<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"/>

答案 3 :(得分:2)

将以下内容保存为index.php,并在名为images的同一目录中创建一个文件夹。记得在服务器上将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>';
}


?>