Uploading image filename into textbox? - php

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
?>

Related

How I can show which image has been uploaded inside input type file with php?

I have database structure as such for image that it stores it name and moves upload to a uploads directory via move_uploaded_file(). I have a page where people can edit what they have uploaded to that specific post. Commonly it is done via giving an value for input but what ever I put inside input is not being displayed. Basically I wants to show which image is uploaded with that specific post.
Html (doesn't works )
<input class="" value="<?php echo $row['post_image'] ?>" type="file" id="addPost_post_image" name="addPost_post_image">
echo $row['post_image'] shows which image name is stored. and is giving right info.
Image is placed under a folder uploads.
Try putting
<img src="uploads/<?php echo $row['post_image'] ?>">
next to input field.
I assume you have an image name saved in the database row post_image. If not then get name from basename($_FILES["addPost_post_image"]["name"]);.

if isset not working with auto image upload

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
}

$_FILES does not accept value [duplicate]

I have a form with text inputs and file inputs; the text fields are being validated. Is there a way to have the form remember which files the user has already selected if they hit submit but need to go back because one of the text fields didn't validate?
You can't "pre-fill" the contents of a file upload field for security reasons. Also, that would mean the file would get re-uploaded every time the form is submitted, which would not be good.
Instead, do this:
Create a file upload field with name file_upload.
On the server-side, process the upload in any case, even if the rest of the form validation fails.
If the form validation failed, but the file was uploaded, insert a hidden input into the form with name file containing the name of the just uploaded file.
Display a user-visible indication that the file is okay. If it's an image, display a thumbnail version of it. If it's any other file, display its filename and/or icon.
If the user chooses to upload a different file in the file_upload field, process the upload and store the new value in file.
Pseudocode:
<?php
$file = null;
if (!empty($_POST['file'])) {
$file = $_POST['file'];
}
if (!empty($_FILES['file_upload'])) {
// process upload, save file somewhere
$file = $nameOfSavedFile;
}
// validate form
?>
<input type="file" name="file_upload" />
<input type="hidden" name="file" value="<?php echo $file; ?>" />
<?php
if (!empty($file)) {
echo "File: $file";
}
?>
Important note
This mechanism can allow any user to claim other user's files as their own, by including a file name that they guessed exists on your server. You will want to ensure that uploaded files are clearly associated with a specific user to avoid this issue.
files input fields are read-only you can't set an initial value for them
You can upload the Files anyway and display the filenames instead of the file select box. To remember the fields, you can use a $_SESSION variable.

How can I download an image based on a url in PHP? curl?

I have used a input file code to upload image to my wallpaper site..
<input type="file" name="img" id="img" />
Is there anyway to select a image without selecting image file..like entering URL to text box?
<input name="img" type="text" id="img" value="http://www.somesite.com/image.my.jpg" size="40" />
I tried its not working...please help me! Anyway to enter image url to text box except select browse button and selecting image file? help me i tries in different ways...
Is there anyway to user type="text" excepts type="file" so We can enter image url in that text box and upload?
With that <input> you then have to tell your PHP code to go and download that file e.g.
<?php
if (!empty($_POST['img'])) {
file_put_contents('savedImage',file_get_contents($_POST['img']));
}
However doing so opens you up to some potential security issues, so be careful!
I think what you want to do is upload an image and then display that on a page.
You need to move the image into a folder on your webserver then display that output.
More info about it here.

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