HTML form for PHP Image Upload Script - php

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

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>

<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

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

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

Related

i think move uploaded file is not working even though he can find if the file is existed

If i choose in the same folder of my target directory it said file exist but when i choose in different folder it said success but there`s no file uploaded in that folder here is my code. i get it in google
<?php
if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "/home/cidinc1802/Pictures/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>
<form name="form" method="post" action="fortesting.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>

When i try to upload files to my web server it doesnt work but if upload them locally it works

I'm trying to upload files to be more precise images from my web-pages to my webserve but it doesn't work. If i do it locally everything works fine but on the Server it doesn't work.
It doesn't give me out any Error message and i don't know why either.
I've tried to give the right permissions and it didn't work it. I tried another way to program it and it didn't work either. It always shows my first else-loop.
<?php
$SBA_ID = $_GET['SBA_ID'];
if (isset($_POST['submit'])) {
$file = $_FILES['my_file'];
print_r($file);
$fileName = $_FILES['my_file']['name'];
$fileTmpName = $_FILES['my_file']['tmp_name'];
$fileSize = $_FILES['my_file']['size'];
$fileError = $_FILES['my_file']['error'];
$fileType = $_FILES['my_file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if(in_array($fileActualExt, $allowed)){
if ($fileError === 0) {
if($fileSize < 1000000){
$fileNameNew = "Auftrag".$SBA_ID.".".$fileActualExt;
$fileDestination = 'AuftragFotos/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("refresh:2;url= ../Startseite.php");
}else {
echo "Your File is too big!";
}
}else {
echo "There was an error uploading your file!";
}
}else {
echo "You cannot uplaod files of this Type";
}
}
My Form
<form action="<?php echo"speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
I expect the output to be that the Image is uploaded to the "AuftragFotos" Dir but it always shows: "You cannot upload files of this Type" even though i specified that Type of file to uploaded.
You have a problem with string escaping.
Change:
<form action="<?php echo "speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
to:
<form action="speichern/Fotospeichern.php?SBA_ID=<?php echo $SBA_ID; ?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>

Could not find the file and upload

I trying to do multiple image upload to the system but it won't able to found the images after upload.
When i try to var_dump the total number of item uploaded it keep showing 0 item uploaded.
Need some help here, Thank You.
This is my HTML code:
<form id="login-form" action="" method="post" enctype="multipart/form-data" >
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
This is my PHP code:
<?php
//Check Error
error_reporting(E_ALL);
//Start Session
session_start();
//Include Database
include_once('dbConnect.php');
if (isset($_POST['submit-scam'])){
echo "button clicked";
if(isset($_FILES['file'])){
/*This Function is to loop multiple upload file into DB*/
$total = count($_FILES['file']['name']);
for ($i=0; $i<$total; $i++) {
$file = rand(1000,100000)."-".$_FILES['file']['name'][$i];
$file_loc = $_FILES['file']['tmp_name'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_type = $_FILES['file']['type'][$i];
$folder="uploads/";
$today = date('Y-m-d');
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
//To Insert Latest base to SQL Latest submit ec_claim id
$sql="INSERT INTO db_evidence(db_evidence_name,db_evidence_type,db_evidence_scam_id,db_evidence_users_id)
VALUES('$final_file','$file_type','1','1')";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
echo "success upload";
// header("Location:index.php");
}
else
{
echo "failed to upload";
}
}
} else {
echo "error no file is found";
}
}
?>
Add enctype="multipart/form-data" to the form
<form id="login-form" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
That should let you read from type="file"

move_upload_file() is not working

<?php
if (isset($_FILES['file']['name'])){
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$location = 'uploads/';
if (!empty($name)){
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded successful';
}
}else
echo 'Please select a file.';
}?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
I am not getting any error but the move_uploaded_file() is not working. It displayed no result on the browser. I have the folder 'uploads/' inside my directory.
The problem is solved, the maximum file size for the file to be uploaded is 2MB. The file that I tried to upload exceed 0.1MB. Thank all of you.

php uploading a file says it is working but does not actually upload a file

I have a website that i would like to upload a file to a temp directory. For some reason the code that i am using is saying that the file is uploaded, but it is not actually uploaded to that directory when i check. I have write permissions on for all users and am still having no luck...
Here is my form:
<form method="POST" enctype="multipart/form-data" action="upload.php">
<p align="center"><input type="file" name="file" size="60"></p>
<p align="center"><input type="submit" value="Submit" name="B1"></p>
</form>
Here is my upload.php file:
<?php
$folder = '/tmp';
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $folder.$_FILES['file']['name'])) {
echo 'File uploaded';
} else {
echo 'File not moved to destination folder. Check permissions';
};
} else {
echo 'File is not uploaded.';
};
?>
It's trying to upload a file to your local folder instead of tmp.
This should fix it:
Change $folder = '/tmp'; to $folder = '/tmp/';

Categories