5

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.';
}
}
1

4 Answers 4

11

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>
Sign up to request clarification or add additional context in comments.

Comments

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>

Bare minimum form to work for you

Comments

2

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

Comments

2

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


?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.