How to remember file loaded while going back? - php

I am working on one web application where i upload one file using upload method in php and in the next page (upload.php), i process the data and display the content but i have one back button.
index.php
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php
<form action="process.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
procees.php
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
<?php
/*process uploaded file*/
?>
when i click Go Back button, again i need to upload the file, if there any way to remember the same file(which i uploaded) even though i go back?

Don't use window.history.back();
just redirect the user to the page you want him to go, and process the image before displaying the button, is good practice

Related

How to send file to $_FILE through html form?

Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}

Image upload anomaly

Recently I changed servers and my images uploading/resizing script (https://www.verot.net/php_class_upload.htm) stopped working, error message "Local file doesn't exist." The only change there was since it worked - I changed from $filetoUpload[$i] to $_FILES["fileToUpload"]["name"][$i].
Here's the script:
HTML
<form action="testupload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[0]" id="fileToUpload">
<input type="file" name="fileToUpload[1]" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
// testupload.php
include('classes/class.images.inc.php');
$pictureslocation = "my images location/";
for($i=0;$i<=1;$i++){
if($_FILES["fileToUpload"]["name"][$i] != "none" AND $_FILES["fileToUpload"]["name"][$i] != ""){
$handle =new upload($_FILES["fileToUpload"]["name"][$i]);
if ($handle->uploaded) {
// Script doesn't get here but gives an error
}else{
echo 'error : ' . $handle->error;
// The error it gives here is "Local file doesn't exist."
}
}
}
Just a simple upload script works without problems.

How to upload multiple files one by one

I need to upload a lot of images at once (e.g. 200 small images) through the browser. I have this code:
<?php
if(isset($_POST['Upload_files']) and $_SERVER['REQUEST_METHOD'] == "POST"){
echo count($_FILES['files']['name']); // Even if I upload more than 20 files, it still displays 20.
$target_directory = "upload/";
foreach ($_FILES['files']['name'] as $file_number => $file_name) {
$file = $_FILES["files"]["tmp_name"][$file_number];
if (mime_content_type($file) != 'image/png' && mime_content_type($file) != 'image/jpeg')
{ $error[] = 'File '.$file_name.' is not in JPG / PNG format!'; continue; }
else
{ if(move_uploaded_file($file, $target_directory.$file_name)) {$count_of_upload_file++;}}
}
}
// If files were uploaded
if($count_of_upload_file != 0){echo 'Your files ('.$count_of_upload_file.' ) were successfully uploaded.';}
// If there was an error
if (isset($error)) {foreach ($error as $display_error) {echo '- '.$display_error.'<br />';}}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="submit" name="Upload_files" value="Upload files">
</form>
And it works. However my provider set the 'max_file_uploads' to 20 (and I'm not able to change it). (The Uploadify extension is working for more than 20 files, but I'd like to have my own small solution.) So I presume I need to add here something, which instead of 200 files at once uploads 200 files one by one (or twenty by twenty). But I don't know how to do it. (To use AJAX? -- I never used it, so I really don't know.) Thank you!
If you don't want to use ajax, you need a loop to receive files
for($i = 0; $i < count($_FILES['files']['tmp_name']); $i++){
$tmp = $_FILES['files']['tmp_name'][$i];
$name = md5(microtime());
if(move_uploaded_file($tmp, "dir/$name.jpg")){
echo "Uploaded successfully";
}else{
echo "failed";
}
}
In your form you must name all your input fields as files[], example:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="submit" name="Upload_files" value="Upload files">
</form>
Or if you want to use ajax here is an example:
Upload multiple image using AJAX, PHP and jQuery

php upload image get undefined index for $_FILE['file'], also submit button is not detected as isset

I have have 2 files. one for image upload. one for evaluating.
1st file:
<html>
<head><title>Upload File</title></head>
<body>
<h2>Upload File</h2>
<form method="post" action="Assignment7_backend.php" enctype="multipart/formĀ­data"><input type='hidden' name='MAX_FILE_SIZE' value='50000'>
Your file: <input type="file" name="image">
<br/>
<input type="submit" value="Send it!">
</form>
</body>
</html>
2nd file:
<html>
<head><title>Upload File</title></head>
<body>
<h2>Upload File now</h2>
<?php
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
if($_FILES['image']['error'] != UPLOAD_ERR_OK) {
print "<p>File not uploaded successfully!</p>";
print "<p><a href='Assignment7_upload.php'>Try Uploading Again</a> </p>" ;
} else {
move_uploaded_file($_FILES['image']['tmp_name'], "./images/" . $_FILES['image']['name'] )
or die("can't move file");
print "<p>Success!</p>";
}
} else {
print("<p>File is not uploaded</p>");
print("<p>".$_FILES['image']['error']."</p>");
if (!isset($_POST['submit'])) {
print("<p>cannot even detect submit</p>");
}
}
?>
</body>
</html>
I was hoping to upload the file, but after I choose the file and submit, it shows error: $_FILE['image'] index is undefined. also, $_POST['submit'] is not set. what is wrong with my code?
The enctype you posted here is wrong. So the form fails to submit any image/file.
I don't know if this is a typo or you actually have it wrong in your code.
The correct one is: enctype="multipart/form-data"

PHP---why can't I upload image with PHP?

The code
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the extension form the filename.
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$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))
{
echo 'you file upload successful.view the file here';
}
else{
echo 'failed';
}
When I upload a JPEG image, it shows the error "The file you attempted to upload is not allowed.". What's wrong with my code?
The main HTML code:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>
Please use in form tag
<form action="upload.php" method="post" enctype="multipart/form-data">
Try replace:
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);
//get the extension form the filename
by
$ext = substr($filename,strpos($filename,'.'),4);
//get the extension form the filename
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<input type="submit" name="sendFile" value="Upload" />
</form>
that should work.
Try with a submit button, <input type="submit " name="upload" value="upload"/>.

Categories