I am a Beginner in PHP and i want to Upload image via PHP and want to save it in destination folder.
i will show you the correct path of the folder where i want the image to be saved, but its only getting saved in database and the image file is not getting save in folder.
The code is as follows:
<?php
if (isset($_POST['submit']))
{
include ('dbconfig.php');
$image=$_FILES['image']['name'];
$name=$_POST['name'];
$detail=$_POST['des'];
if($image!=null)
{
move_uploaded_file($_FILES['image']['tmp_name'],"/Tomar/php/a/uploads/".$_FILES['image']['name']);
}
$entry = "INSERT INTO aboutstudent(stuimg, stuname, studetail) VALUES ('$image','$name','$detail')";
$result = mysql_query($entry);
}
?>
and i want to save my images at below path:
D:/wamp/www/Tomar/php/a/uploads
You can get code example from Image Upload in PHP
make sure you have used enctype="multipart/form-data"
<form action=" " method="post" enctype="multipart/form-data">
and your "uploads" directory has 777 permission.
Referrence : File upload in php
Related
I am uploading an image using an HTML form.
When I get the full path to the image, it outputs something like this:
'/tmp/phpkIv1BY/10259944_770025219687938_1184503840380306483_n.jpg'
but when I go to the /tmp folder, the sub-folder phpkIv1BY doesn't even exist! What's going on here?
Reason for this behaviour
All uploaded files are temporarily stored in the folder location as defined in
(In php.ini)
upload_tmp_dir =
(Read more about this: http://php.net/upload-tmp-dir)
These temporarily stored files are no longer exist after that php script execution(As mentioned in previous answer request life span) finished.
Do the following to view uploaded file while script executing.
upload.html
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
upload_file.php
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
$fp=fopen("/tmp/write.log","w+");
fputs($fp,"Original Name:".$_FILES["file"]["name"].";temporay Name:".$_FILES["file"]["tmp_name"]."\n");
fclose($fp);
sleep(50000);
}
?>
Upload file by upload.html
In another terminal apply tail in /tmp/write.log after uploading image(Because sleep time is too much so you able to find the image)
#tail -f /tmp/write.log
Copy that temporary file into any place and place the extension of original image file
For example my printed log line is
Original Name:digits.png;temporay Name:/tmp/phpOKlpoK
(Needed to do this with root privilages)
#cp /tmp/phpOKlpoK /home/sandeep/Desktop/file.png
#chown sandeep.sandeep /home/sandeep/Desktop/file.png
So this is the uploaded file that you.
(
Instead of doing all these steps for checking uploaded images you can use move_uploaded_file()
)
I can not find anywhere if PHP might create a temporary folder for temporary uploaded files. But what is certain is that uploaded files are kept temporary and as soon as the request is done, they are removed. So if you think you can find some uploaded file in the /tmp folder, think again.
If you would like some uploaded file to live longer than a request life span, then you need to move it somewhere safe using move_uploaded_file().
I understand the include function in php where the directory has been determined, like this:
include (dirname (__FILE__). '/file.php');
my question: can I use this function if the file from submit form?
my source code:
upload.php
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
process.php
<?php
include (dirname (__FILE__). '??????????');
?>
Bad english, sorry
If you really want to do this, you can do:
include $_FILES['file']['tmp_name'];
Uploaded files are not put in the same directory as the script, so you don't need dirname(__FILE). The tmp_name field contains the full pathname to the uploaded file.
One does not simply run a file uploaded by a user.
That being said, the details of the uploaded files are stored in the $_FILES array. The temporary name (and path) of the uploaded file is $_FILES['file']['tmp_name'].
You could either include this file directly:
include $_FILES['file']['tmp_name'];
Or, you could store this file first, using move_uploaded_file and then include that file:
$destination = "path/to/file.php";
move_uploaded_file($_FILES["file"]["tmp_name"], $destination);
include $destination;
Good luck!
I've got the following code to upload a file and add it to the folder called images located in the root of the server.
$file = $_FILES['prodImg']['tmp_name'];
$newLoc="/images/" . $_FILES['prodImg']['name'];
if(move_uploaded_file($file, $newLoc)){
//do some other code here
}
else{
echo 'error';
}
the form has this button to add the image
<input type="file" name="prodImg" id="prodImg" accept="image/png" />
the images folder has all permissions set to read, write and execute
every time i try to upload an image it go to the else statement.
not sure what i'm doing wrong here.how do i make it work properly?
Do you have attribute in your form?
enctype="multipart/form-data"
I.e.:
<form action="..." method="post" enctype="multipart/form-data">
Then you can try to check permission for that folder.
And also try change filepath:
$newLoc="./images/" . $_FILES['prodImg']['name'];
or
$newLoc="images/" . $_FILES['prodImg']['name'];
The path of your $newLoc variable may be wrong. Try ./images/.
The one specified may define an image folder on the same level as the home, root... folders if you use Linux.
remove the slash ahead of images
$newLoc= "images/".$_FILES['prodImg']['name'];
I am currently develop a student database system for my faculty. I am using the PHP together with MySQL. I am thinking to create an option for student to upload their profile photo but I could not find any proper instruction or tutorial of doing that.
Here's the code processing the file uploading:
<?php
/* Script name: uploadFile.php
* Description: Uploads a file via HTTP with a POST form.
*/
if(!isset($_POST[‘Upload’]))
{
include(“form_upload.inc”);
}
else
{
if($_FILES[‘pix’][‘tmp_name’] == “none”)
{
echo “<p style=’font-weight: bold’>
File did not successfully upload. Check the
file size. File must be less than 500K.</p>”;
include(“form_upload.inc”);
exit();
}
if(!ereg(“image”,$_FILES[‘pix’][‘type’]))
{
echo “<p style=’font-weight: bold’>
File is not a picture. Please try another
file.</p>”;
include(“form_upload.inc”);
exit();
}
else
{
$destination=’c:\data’.”\\”.$_FILES[‘pix’][‘name’];
$temp_file = $_FILES[‘pix’][‘tmp_name’];
move_uploaded_file($temp_file,$destination);
echo “<p style=’font-weight: bold’>
The file has successfully uploaded:
{$_FILES[‘pix’][‘name’]}
({$_FILES[‘pix’][‘size’]})</p>”;
}
}
?>
Code for the file upload form:
<!-- Program Name: form_upload.inc
Description: Displays a form to upload a file -->
<html>
<head><title>File Upload</title></head>
<body>
<ol><li>Enter the file name of the product picture you
want to upload or use the browse button
to navigate to the picture file.</li>
<li>When the path to the picture file shows in the
text field, click the Upload Picture
button.</li>
</ol>
<div align=”center”><hr />
<form enctype=”multipart/form-data”
action=”uploadFile.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE”
value=”500000” />
<input type=”file” name=”pix” size=”60” />
<p><input type=”submit” name=”Upload”
value=”Upload Picture” />
</form>
</div></body></html>
I got the same outcome which I cant find the file that being uploaded and it is not being uploaded to the location as it should be.
You should change the destination:
$destination=’c:\data’.”\”.$_FILES[‘pix’][‘name’];
Or, if this is not working, try to move the uploaded file somewhere near the script path, like:
$destination= dirname(__FILE__).DIRECTORY_SEPARATOR.$_FILES[‘pix’][‘name’];
If the second one works then is means, you have given a wrong directory for the upload.
I think you should first change your image path.
and than your datatype set to image in mysql manually,
it will be done.
Do you get any error messsage? Do the PHP user have write permissions to the upload directory?
Also, here's two advices possibly not related to your question:
Don't use eregi in any new code. It is deprecated, which means it will be removed in a future version of PHP. Instead, use the preg_ functions, or just strpos in this case.
Why would $_FILES[‘pix’][‘tmp_name’] ever be the string "none"?
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