Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form for PHP Image Upload Script

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.

Here is php code:

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.';
}
}
like image 592
user3026799 Avatar asked Nov 24 '13 07:11

user3026799


4 Answers

I came across this exact code a while back Here you go for 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>
like image 91
Mario Segura Avatar answered Sep 18 '22 18:09

Mario Segura


<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

like image 35
Satya Avatar answered Sep 18 '22 18:09

Satya


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:

<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"/>
like image 44
Hasib Mahmud Avatar answered Sep 21 '22 18:09

Hasib Mahmud


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.

<?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>';
}


?>
like image 43
cardazio Avatar answered Sep 18 '22 18:09

cardazio