I'm sure I'm doing something silly but I've checked and re-checked this. This is my photo uploader, meant to save the photo file itself to a folder (named "reccs"), and then save the name of the photo to a row in my DB. The DB part works perfectly, but my photo isn't showing up in the folder.
It displays no errors and appears to be working (I get the "success" message). Ack!
Code is below. Thanks for ANY insight.
$link = mysql_pconnect($host, $username, $password);
$db = mysql_select_db ($dbname);
$target = "reccs/";
$target = $target . basename($_FILES['photo']['name']);
$pic=($_FILES['photo']['name']);
mysql_query("UPDATE login SET recc = '$pic' WHERE username = '".$_SESSION['user']['username']."'");
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "The file has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
try;
if(move_uploaded_file($pic, $target))
Depending on your server setup you may have to set $target to be an absolute path and not relative. At least it should be absolute to your home dir, so if reccs is a dir in your root site directory then set $target to /reccs/file.name.
The reason for this is that files are uploaded by defualt to /tmp, which is at the root (OS) level, not the hosted level, so paths relative to /tmp are generally meaningless.
saving photo with same name is very bad practice. I will suggest you to do below before uploading the foto.
extract name and extension.
now check that extension is valid or not. if valid then go further
first trim and then clear all multiple spaces with single spaces.
now lowercase filename and change all space with _ and append timestamp with filename.
remove all special characters ( ', " , \ etc )
now add filename with that extension .
then store into database
Should reccs have a forward slash in front of it? It would seem the mysql update query would be better inside the if statement , after you know the move_upload_file was successfull.
Related
I have a hosted website and i want to upload my images to image directory and link the image to mysql database .
What i did:
Creating form as :
<form method=POST align=center enctype="multipart/form-data">
<input type=file name=image>
Php code :
$uploaddir = 'images/';
// echo $uploaddir;
$target = "$uploaddir".basename($_FILES['image']['name']);
echo $target;
$image = $_FILES['image']['name'];
$sql="insert into News2(image_News2)
values($image)";
$res=mysqli_query($con,$sql);
copy($_FILES['tmp_name']['name'],$target,true);
What this code actually do is only inserting the image into the database not copying the image into the images folder.
What i searched for and found :
You shouldn't specify a http:// URL as $uploaddir but a path relative to the path where the php script is running from.
So i what i really wanted to do is :
1- Copying the image into image folder directory on website.
2- Linking the copied image to the database.
you're missing the name ïmage"
not:
copy($_FILES['tmp_name']['name'],$target,true);
but:
move_uploaded_file($_FILES['image']['tmp_name'], $target);
And maybe you should try the move first, before inserting it in the database, should that action fail (or filesize = 0) etc.
do check on both the file actions and the db actions, should they fail: rollback
extra:
is only 1 column present in your database table?
is it wise to use the uploaded file's name? Duplicates can happen.
I would after checking the file insert the original name into the database. Get the autoincrement id of that record. rename the file to ./1234.jpg
through the database you can then later use the original name. But this numbering might go too var for a simple application
PHP Code :
if (isset($_FILES['image']['name']) && $_FILES['image']['error']
== 0) {
$uploads_dir = '/uploads';
$temp_file = $_FILES['image']['tmp_name'];
$name = basename($_FILES["image"]["name"]);
if(move_uploaded_file($temp_file, "$uploads_dir/$name")){
//Fire Insert Query For Insert image name in your database
}
else
{
echo "Error !! , Your image not uploaded.";
}
}
?>
as per your given data you must close Form tag then give form action ="" attribute to specify url . and if your oprating system is linux then check folder permission in set or not , also check your database table if field name is set unique or not ?
then write below code in given url file
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
Previously, when I tried uploading image into the database, the image won't display. When I check the path in the db and in the folder, it is correct.
Correct path in db and folder.
And then when I tried to view the image that has been uploaded it says that I don't have the permission to view it.
I have also tried uploaded different photo extension and different photo viewer application and I still cannot view the image. Apart from that, I have tried
W3School PHP5 File Upload. Again same thing happen, I cannot view my image.
This is my code :
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location= $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"];
move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"]);
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
Why can't I view my image? Is it because of the document root? Or is it something else? Please help me thank you.
UPDATED :
Based on the image below, my code (as shown above) is inside the admin folder. The reason why I would like to save my images in /ehars/photos so that, every level of user, admin admin2 and user can view the same photo that has been uploaded. If you could advice me what is the best way to do in order to achieve my objective above. Thanks again!
If your URL scheme is not "file://", you should authorized your browser.
I remember that you can't easily link CSS and image to the local machine due to security reasons.
change your code into this
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location='/ehars/photo/' . $_FILES["image"]["name"]; //remove $_SERVER['DOCUMENT_ROOT']
move_uploaded_file($_FILES["image"]["tmp_name"], '/ehars/photo/' . $_FILES["image"]["name"]); // remove $_SERVER['DOCUMENT_ROOT'] .
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
why tou should change your code, because your server not gonna read windows path (c:/apache/htdocs/yourimagespath/yourimages.jpg); it should read (/images/yourimages.jpg), i asume htdocs is your root directory. and the result in your database is /ehars/photo/yourimages.jpg not c:/apache/htdoc/ehars/photo/yourimages.jpg.
hope it help you.
Got the server, got the domain, got the code, getting the images successfully, making the products for the customers from the image files they upload. Yay!
Problem: all my image names are image_0001 etc.
Customers can't rename image files from iPhones and do not care to from PCs.
So I was thinking about putting a short form on the upload page asking for customer's last name and having the PHP code attach that name to the image file(s) being uploaded.
If it's not possible, I'm sorry for the inconvenience.
You can rename files after they have been saved to your server, check out the PHP manual for the rename function - http://www.php.net/manual/en/function.rename.php, or just while you are moving them from the tmp directory, you can specify a different name for the uploaded file. See http://www.php.net/manual/en/function.move-uploaded-file.php
Be careful to include something in your code for dealing with naming conflicts.
This one might help :
$imagename = basename($_FILES['file']['name']);
$ext = pathinfo($imagename , PATHINFO_EXTENSION); //we want to change the file name but not the extension
$newImagename= $imageName.$username.'.'.$ext; //assuming you hold the username in $username
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$newImagename}"))
{
....
}
I am working with adding a file upload system to my website and I cannot seem to get the file links to match up with the file name that is generated. I am adding a time function onto the front of the file name to make it unique (it generates a number based on the time). It does do this, but for some reason, that unique name is not saved in the database. Could someone help me figure this out?
//This is the directory where images will be saved
$target = "files/";
$target = $target. time(). basename( $_FILES['photo']['name']);
echo $target;
//postThis gets all the other information from the form
$tfid=$_POST['tfid'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$hca=$_POST['hca'];
$file=($_FILES['photo']['name']);
//Writes the information to the pic table
mysql_query("INSERT INTO pic
(tfid, file)
VALUES ('$tfid', '$file')")
or die(mysql_error());
ECHO "<strong>pic table has been saved.<br></strong>";
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "<strong>The file has been uploaded</strong>";
}
else {
//Gives an error if its not
echo "<strong>Sorry, there was a problem uploading your file.</strong>";
}
echo '<br>';
require 'insertbuttons.php';
I see what was going on now. Sorry about the comments. You'll want to save the $target variable in your database. The posted variables won't match the target. So, where you have $file in your SQL statement, you'll probably want $target instead. That should give you the name of the file, but without the extension it looks like.