if isset not working with auto image upload - php

I have a form that uses JQuery to auto submit the form immediately after the file has been selected.
The image is uploaded and displays correctly but no data is inserted into the table.
I have tried removing the 'if isset' to test the SQL inserts data correctly and it does but obviously the if statement is needed so it doesn't insert data to the table every time the page is loaded.
I assume the problem is because I have removed the forms submit button however if I add it back in the form no longer auto submits after image has been selected.
$('#file').change(function() {
$('#target').submit();
});
if(isset($_FILES['file'])){
DB::query('INSERT INTO images VALUES(\'\', :image, :img_id)',array(':image'=>$image, ':img_id'=>$userid));
}
<form id="target" action="upload.php" name="target" method="POST" enctype="multipart/form-data">
<img src="uploads/profileu<?=$userid?>u.jpg?=<?php echo rand() . "\n";?>">
<label class="btn btn-primary glyphicon glyphicon-pencil">
<input type="file" id="file" name="file" onchange="form.submit()" style="display:none">
</label>
</form>
I should also note the images are uploaded to and displayed from a local folder (working correctly) and the the SQL is to simply insert a 1 or 0 into database (user has image or does not have image)

The underlying issue was that I had the form action="upload.php" which is where the PHP for uploading images was all stored. Which meant I had to move my if(isset) statement to the top of that page instead of the page where the form was. Now it works perfectly without a submit button.

Because you're inserting $image and $userid you should check if those variables are set.
Like so:
if (isset($image) && isset($userid)) {
// insertion code
}

You have to use $_FILES['fileName'] not $_FILE
To Check if a file has been successfully uploaded use.
if($_FILES["fileName"]["error"] == 0){ ... }
Along with this you can check if the uploaded file is an image you can check it as:
if(getimagesize($_FILES["fileName"]["tmp_name"])){
//Its an Image
}
else
{
//Invalid Image
}

Related

how to insert default image into database with PHP

UPDATED:
I have a form which have image upload field which have not required field. Now i want to if anyone submitting form without uploading image then a default image will be inserted into database. How can i do that with PHP?
<form method="POST" action="{{ route($base_route.'.eventEdit', ['event_id' => $event->event_code]) }}" accept-charset="UTF-8" id="general_form" novalidate="novalidate">
<input name="name" type="text" value="something" class="name">
<input name="name" type="file" value="DefaultImageName" class="image">
<button class="btn btn-default"> Submit </button>
</form>
you could put a defult value in the image column in your database, this way all users will get this image, unless you specify a different image, because it will override it...
this method worked for me...
With jQuery you are not able to do this, you can just send data to the server and process it on serverside. Eg. Save to database.
jQuery is a Javascript library, running in your web-browser. It does not have access to your database and should not have access to your database.
Your application server has access to your database and there you should default the image.
If your image is a field of a table where you insert anyway and you want to specify a default image when a record is inserted, then you need to alter your table and specify a default value for your images. In this case your problem can be solved solely by doing such an alter.
If your case is not as simple as above, then on your server where you insert the images you may check whether the image was posted and if not, then insert a default image.
Also, you duplicate the name of your tags, you might want to specify different values for the name attribute for your two inputs.
$imageName = "default.jpg";
if (isset($_POST["name_of_the_image_file"])) {
//move functionality of the image file
$imageName = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
}
$sql = "UPDATE table_name SET name='Doe' and image_field='$imageName' WHERE id='$id'";
$conn->query($sql);

Uploading image filename into textbox?

I have uploaded image filename into database and retrieved from database perfectly. Now i have a problem with updating that image filename. so, what I thought is getting the uploaded image name into textbox and storing that to database so I need to get filename of uploaded image into textbox using php.
Thanks in advance
Well what you can do in the situaltion you mentioned in comment is.
Check for file input type value. if it posted black then store the hidden name from the form.
Example
HTML
<input type="hidden" name="old_image" value="MySQL Query Old Result" />
<input type="file" name="image_url" />
PHP CHECKING
<?php
if(empty($_POST['image_url']))
{
$save_image_to_db = $_POST['old_image'];
}
else
{
$save_image_to_db = $_POST['image_url']['name'];
}
//update query with $save_image_to_db
?>

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/

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.

PHP file form question

My Code :
<?php
function dbAdd($first_name , $image) {
//mysql connect database code...
mysql_query("INSERT INTO users SET first_name = '".$first_name."', image = '".$image."'");
$mysql_close($sql);
}
if($_SERVER['REQUEST_METHOD']=='POST') {
dbAdd($_POST['first_name'], $_POST['image']);
}
?>
<form enctype="multipart/form-data" method="post" action="">
First Name : <input type="text" name="first_name" >
Image : <input type="file" name="image">
<input type="submit">
</form>
The form "file" is to upload. I know that. But I wonder how to get the values so I can put the path of image in the database. The code is already working. The $first_name can already save to the database.
Thank you for the answers.
Jordan Pagaduan
The file will be uploaded to a temporary place on the server when the form is submitted.
Once the form has been submitted, the $_FILES variable will contain all the files submitted. In your case, you could access the uploaded file using $_FILES['image']. Most likely you will want to move the file out of the temporary directory to a safer place.
For more info, have a look at the PHP manual on the topic, specifically the page on handling POST uploads. That second page has an example for you on how to move the uploaded file (have a look at the move_uploaded_file() method).
Straight from W3C: Upload form & $_FILE variable

Categories