Resetting check box value when 'back' is hit - php

I have a site that allows user to upload files to a folder. First I check if the file exists, if it does then I check the value of a checkbox to determine if the user wants to overwrite the existing file. If upload is hit and the box is unchecked, I do a die() that has a message and a back button. However, when I hit back, check the checkbox (the file is still displayed as if it was selected), then submit again, it doesn't overwrite the file (but appears to, no die). Is the checkbox value not being changed or reset when I hit back or something?
Maybe worth mentioning, there is a form (if method=post) and a form for (else) but they are exactly identical.
Can someone help me out please? Thanks.
Here is my code:
Form:
<form id='upload' method='post' enctype='multipart/form-data' action='<?php $_server['PHP_SELF']; ?>'>
<input type='file' name='file_upload'>
<br><br>
<label><input type="checkbox" id="overwrite" name="overwrite" value="Yes"> Overwrite file (if file exists)</label>
<br><br>
<input type='submit' value="Upload">
</form>
Rest:
if(file_exists($fullFileName)){
$confirmation = $_POST["overwrite"];
if($confirmation == "Yes") {
unlink($fullFileName); //overwrite confirmed, unlink old file
}
else {
die("Upload aborted. Please check 'Overwrite file (if file exists)' box to overwrite existing file <input type='submit' href='#' onclick='history.back();' value='Back'>");
}
}
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '' . $_FILES['file_upload']['name'])){ //move new file into place
die('Errrrrr! Error uploading file - check destination is writeable.');
}

Instead of a die(), you can just do a PHP header redirect which would essentially "clear the form" and is would be a new page load. If you wanted to retain the selected file, you could pass it back as a GET value.

Related

Ajax form upload with text input

I'm trying to submit a form with several file
uploads using ajax with input text.
If the user have uploaded all the
files using ajax but forget to enter his
name and the form re-populate again. How do
i tell the user that he has already uploaded
these files
<input type=file id=file1 name=file1 />
<input type=file id=file2 name=file2 />
<input type=text id=name name=name />
The form reload showing error message that
the name has not being enter. How do i tell the
user that the file1 has already being uploaded.
Technology(PHP)
Thanks
I suggest writing code that:
1. When the form is activated, check what is there and what isn't.
2. Cache that data into cookies
3. Do uploading code
4. Upon reloading the form, check cookies for what was entered and what wasn't.
5. Display an alert/message to tell them what they entered and what is still needed.

How do I replace the submit button after checking if a file upload field is empty?

I have a file upload form:
<form method="post" enctype="multipart/form-data">
<input type="file" name="image_1" />
<input type="submit" name="upload_photo" id="upload_photo" />
</form>
I have a php script that uploads the file, but I noticed that whenever I upload a larger file, it may take up to 10 seconds for the form to submit and the page to reload. After the user clicks on the submit I would like for the submit button to be replaced with a message. I have written some jQuery that makes this work:
$("#upload_photo").click(function() {
$("#upload_photo").replaceWith('<p>Image is uploading. Please wait...</p>');
});
This replaces the submit button just fine, but the problem is that it will also replace the submit button even if the file upload field is empty. Therefore, after the user clicks the submit button, I need to check if the field upload field has a value, and if it does to replace the button with the message. I have tried that here:
if($_FILES['image_1']['tmp_name'] != '') {
$image_uploaded = true;
}
if($image_uploaded == true) { //If a file has been uploaded
echo '<script type="text/javascript">
$("#upload_photo").replaceWith("<p>The image is uploading. Please wait...</p>");
</script>
';
}
Unfortunately, that doesn't work and the file uploads without replacing the submit button. I have tried substituting the .replaceWith line with an alert, but the form will upload the image, reload the page and then display the alert which is far too late.
How do I replace the submit button after checking if a file upload field is empty?
Just check to see if there is a value in the file input field
if ($("[name=image_1]").val()){
$("#upload_photo").replaceWith('<p>Image is uploading. Please wait...</p>');
}
I often handle this by doing this:
1) Disable the submit button
$(this).attr('disabled', 'disabled')
and 2) Change the text on the submit button.
$(this).attr('value', 'Please wait ...')
Here is a jsfiddle of this.
http://jsfiddle.net/Vqyc6/8/

Using only PHP and HTML to act like JavaScript

I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.
We need to get an uploaded file as a temp file only (index.php)
Output size of file (upload.php)
User can confirm save of file or not (upload.php)
So, I have the majority down, but my problem lies with creating the temp file into a permanent file.
index.php
<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php
<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
}
?>
<form action="" method="POST">
<input type="submit" value"YES please save">
<form>
<?php
if (isset($_POST['submit']))
{
//Code for saving file
echo 'File saved!';
}
?>
Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.
Hopefully the following comments can help you with the part you are stuck on.
In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.
One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.
Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.
I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.
Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.
Hope that helps.
Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:
<input type="submit" value"YES please save">
Should be
<input type="submit" value="YES please save" name="submit">
Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.
The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.
This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.

PHP: File upload always returning nothing

Hey guys I'm working on a file uploader and I have come across a problem. In my code I am checking to see if a file has been selected via the file upload form, here is the form code:
<form method="post" action="actions/save.php?id=<?print($id);?>" enctype="multipart/form-data">
Listing Photo: <input type="file" name="file"/>
<input class="add" type="submit" name="submit" value="Save"/>
</form>
The user selects the file to upload then clicks the "Save" button. Now in my uploading code i am trying to check if the file form has been set like this:
$file = $_POST['file'];
if(isset($file)) {
//Continue
} else {
//Go back
}
Now my problem is that even if the file input is set (File selected) it goes to the "Go back" part of the code.
Any suggestions or a different way of checking?
Any help is appreciate, Thanks.
When you upload files through form, you should have $_FILES superglobal array with that file, so try
print_r($_FILES['file'])
to see what it cointains (size, error code, path ...)
Uploaded files end up in $_FILES, not in $_POST
see: http://nl.php.net/manual/en/reserved.variables.files.php for documentation and examples
You should have access to uploaded files using the $_FILES array. See also the reference documentation.

In the next page $_FILES["myfile"]["tmp_name"] = NULL

I had upload file with POST and it seems to works fine, but after I go to the next page I can't use the file with the location in the global varible: $_FILES["myfile"]["tmp_name"]
because it's null. I don't know why, I used this code before and it worked fine...
Here is the code:
www/step1.php
if (isset($_POST["check_if_press"]) && $_POST["check_if_press"] == "Upload")
{
if (!empty($_FILES["myfile"]["tmp_name"]))
{
header("Location: ./step2_1.php");
}else echo "Please select a file";
}
<form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data">
Upload file: <input type='file' name='myfile' /><br />
<input type='Submit' name='check_if_press' value='Upload' />
</form>
www/step2_1.php
echo $_FILES["myfile"]["tmp_name"];
Now I get NULL printed on the screen.
When I used POST without the arguments :
action="<?php echo $PHP_SELF
But with:
action="./step2.php"
Instead, It work, but than I cant use the upload check .
Thanks,
Yonatan.
The file is only present in $_FILES within the scope of the post. What's happening here is:
User is posting a file to step1
step1 is doing something with the file, then telling the user to go to step2 (by means of the location header)
In response to the location header, user is making a GET request to step2, no posted file is associated
You're going to need to either post the file directly to step2 (which you said worked, just didn't have the logic of step1) and put your server-side file checking logic there, or in step1 store the file somewhere on the server (either save it to the file system, store it in a database, maybe store it in session which I don't recommend, etc.) where it can be accessed by step2.
step2 is a completely separate request from step1 and doesn't have access to anything within the scope of step1.
The temporary file is deleted at the end of your post script, so you'll need to copy it to another location before you redirect:
if (!empty($_FILES["myfile"]["tmp_name"]))
{
//COPY THE FILE TO ANOTHER LOCATION HERE...
header("Location: ./step2_1.php");
}else echo "Please select a file";
}

Categories