I have a problem with moving uploaded files.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
$sql = "INSERT INTO items (name , description,`price`, `country`, `release`, `condition`, `image`)
VALUES ('$name','$description','$price', '$country', '$date', '$condition', '$targetFileForItem')" ;
?>
the variable $targetFileForItem works currect, and inserts into my db very well, but the file don't move into $target_filevar's folder, which is uploads. As you see I use move_uploaded_file() function, but i's not working. Any suggestions?
Write this to debug
ini_set('display_errors',1);
error_reporting(E_ALL);
If your code is ok then check file permissions you can use this
if (is_dir($target_file ) && is_writable($target_file )) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
is_writable Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable
for more info read this http://php.net/manual/en/function.is-writable.php
Check your permission for upload folder it must be 775. If you are using FTP than right click on folder and change File permission of that folder to 755.
If it's localhost then it must be a path or folder name issue.
And make your code like this so you can get find out errors also.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
// if folder not exists than it will make folder.
if(!file_exists($target_file))
{
mkdir($target_file, 0777, true);
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file))
{
echo "file successfully uploaded";
}
else
{
echo "error in file upload";
}
?>
Related
So i want my page to show the image whose path I am getting from the mysql database and displaying on the same screen. This is my code, I have tried everything, please let me know where I'm going wrong.
while ($row = mysqli_fetch_array($return_data)) {
echo "ID:".$row['demo_id']."<br>";
echo "Name: ".$row['demo_name']."<br>";
echo "Version: ".$row['demo_version']."<br>";
echo "Details: ".$row['demo_details']."<br>";
echo "File Link: ".$row['file']."<br>";
$new = $row['file'];
echo '<img src = \"$new\"/>';
}
mysqli_free_result($return_data);
echo "Data retrieved successfully!"."<br>";
?>
<img src = "<?php echo $new?>">
echo "File Link: " returns me the whole path of the uploaded file.
How do I render the image at that path in the same page?
neither of the image tags are working. Thanks in advance!
edit
File Link: C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg
this is the path I get as an output.
Basically this is the folder where I have uploaded the image from another php file
<?php
//this module is used to temporarily store the uploaded file to the server
$target_dir = "random/"; //we randomly assign a value to the upload target directory
$target_file = $target_dir . basename($_FILES["image_file"]["name"]); /*here ["name"] is the original name of the file before it was updated
target file is assigned this name by appending it to the $targer_dir
now target_file is the uploaded file name along with the path*/
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);/*this returns various kind of info about the path for eg Directory, Basename
in our case it returns extension of the file*/
//this sub module is to check whether the file is really an image file
if(isset($_POST["submit"])) { //isset is used to confirm whether the value has actually being submitted
$check = getimagesize($_FILES["image_file"]["tmp_name"]);/*here ["tmp_name"] is the location of temporary file on server
getimagesize confirms image format by returning dimensions etc*/
if($check !== false) {
echo "A file is of an image format<br>";
}
else {
echo "The file is not an image!<br>";
}
}
//Test module to upload files to a destination directory and check whether they have been uploaded or not
if (is_uploaded_file($_FILES['image_file']['tmp_name']) && $_FILES['image_file']['error']==0) { /*code to check two things: 1. whether the file exists in the temp memory 2. whether the file has any error*/
$path = 'C:\Apache24\htdocs\demo_webpages_project\neweruploads\\' . $_FILES['image_file']['name']; /*this sets the destination directory(along with expected file name)*/
if (!file_exists($path)) { //if the file does not exists at that path
if (move_uploaded_file($_FILES['image_file']['tmp_name'], $path)) { //use the php file move function to move it
echo "The file was uploaded successfully."; //success
}
else {
echo "The file was not uploaded successfully."; //failure
}
}
else {
echo "File already exists. Please upload another file."; //detects existence of file with exact same name
}
}
else {
echo "The file was not uploaded successfully."; //if any problem with original uploading
echo "(Error Code:" . $_FILES['image_file']['error'] . ")";
}
?>
Does this help?
edit 2
http://localhost:8080/demo_webpages_project/download.php?project_name=footer&version=&submit=Search
this is my local directory path.
the solution you provided is allowing me to read images which are in the demo_webpages_project folder pointing directly there), not to neweruploads folder
If your uploaded files are stored in the neweruploads subdirectory, then replace this code:
$new = $row['file'];
echo '<img src = \"$new\"/>';
By this one :
$new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
echo '<img src = \"neweruploads/$new\"/>'; // FILENAME WITH UPLOAD PATH.
▲
I've run into a problem where I need to upload an image into a folder and store its path into database.
If the folder is not present then create the new folder and then store it there and save full path into database.
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
image name to be used
$imagename=("Userimage")."-".time().$ext;
image path
$target_path = "images/".$imagename
Condition
if(move_uploaded_file($temp_name, $target_path))
{
$query_upload="INSERT into images_tbl (images_path , submission_date) VALUES ('".$target_path."','".date("Y-m-d")."')";
$imagedb= mysqli_query($con,$query_upload);
while($imagepath = mysqli_fetch_array($imagedb))
{
echo "success";
}
}
It's easy:
$target_path = 'images/'.$imagename;
if(!is_dir($target_path)) mkdir($target_path, 0755);
Don't use " " when you can use ' ', because each time you use " .." , php try to find variable inside and lost time for nothing. ;)
You could do something like this:
<?php
$target_dir = preg_replace('#^(.*)'.basename($target_path).'$#', '$1', $target_path);
if (!is_dir($target_dir))
mkdir($target_dir, 0755);
if(move_uploaded_file($temp_name, $target_path))
{
$query_upload="INSERT into images_tbl (images_path , submission_date) VALUES ('".$target_path."','".date("Y-m-d")."')";
$imagedb= mysqli_query($con,$query_upload);
while($imagepath = mysqli_fetch_array($imagedb))
{
echo "success";
}
}
?>
The preg_replace part strips the directory path out of the complete path.
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
0777 is the directory permission ,that is a creating the directory and setting the permission to 0777 so that file can be uploaded to this directory
Need to insert uploaded file into respected folder
Here i am creating folder based on unique id.
I am not able to insert file into the folder.
when file is uploaded both file and folder are stored separately.
if ($file_check_error == 0){
if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){
echo"inside 2"."<br>";
echo"Your File Successfully Uploaded";
}
$path_user = '/home/devestctrl/public_html/wp-content/plugins/est_collaboration/Files/'.$send_id;
if (!file_exists($path_user)) {
if (mkdir( $path_user,0766,false )) {
$path_move = $path_user."/".$path;
echo $path_user;
rename($path,$path_move);
echo "Success+++++++++++";
}
else{
echo $path;
echo "Failure+++++++++++";
}
}
}
$send id is unique id.
Please let me know where i have gone wrong?
You are messing up your logic. First you move your uploaded file
if (move_uploaded_file($file['tmp_name'], $upload_directory.$path)) {
and only than you try to create new directory
if (mkdir($path_user,0766,false )) {
and only if current user never uploaded anything you rename file moving it to other dir
rename($path,$path_move);
Correct logic:
Format uploaded file path $path = $upload_directory.DIRECTORY_SEPARATOR.$send_id
Check if dir exists file_exists($path)
If not exists, create it mkdir($path, 0766, false)
Upload file move_uploaded_file($file['tmp_name'], $path)
E.g.:
$path = $upload_directory.DIRECTORY_SEPARATOR.$send_id;
if (!file_exists($path)) {
mkdir($path, 0766, false);
}
move_uploaded_file($file['tmp_name'], $path);
I can insert the folder path into the database but the picture file doesn't seem to move into that folder?
$pic = $_POST['pic'];
$picName= $_FILES['pic']['name'];
$type = $_FILES['pic']['type'];
$tmp = $_FILES['pic']['tmp'];
$picPath = "/pictures/";
if(is_uploaded_file($tmp)) {
if(move_uploaded_file($tmp, $picPath . $picName)) {
echo "congrats! Image is uploaded.";
}
else {
echo "Sorry, couldn't move your picture.";
}
}
else {
echo "Sorry, couldn't upload your picture.";
}
$picPath = $picPath . $picName;
mysql_query("INSERT INTO User(pic) VALUES ('$picPath')");
I get this echo message: Sorry, couldn't upload your picture.
The php files is saved on public_html folder, and I have a pictures folder where I want to move the users pictures into.
The insertion works as I can store the $picPath in my database, but the picture don't get stored in my folder.
Try replacing
$tmp = $_FILES['pic']['tmp'];
with
$tmp = $_FILES['pic']['tmp_name'];
1) check folder permissions if its writable or not.
2) make sure your path is same with the same folder name in code as well.
3) Try to change from this $picPath = "/pictures/"; to something like this $picPath = "pictures/"; removed forward slash.
I am unable to move file into desired folder. I want to save image into uploaded folder. In mysql i have it in blob type.
this is my code
$target_Path = "uploaded/";
$target_Path = $target_Path.basename( $_FILES['image']['name'] );
move_uploaded_file( $_FILES['image']['tmp_name'], $target_Path );
mysqli_query($con,"UPDATE info SET photo='$target_Path' WHERE user_id='$id'");
In mysql it is showing that something has been saved but it not opening and the file is not moving into uploaded folder. i am doing this in my localhost. Please help.
Try wrapping your move_uploaded_file with an IF statement, and throw an exception if the file fails to transfer:
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_Path ) === false) {
throw new Exception([text]);
}
Alternatively, to test whether the destination is writeable, you could try opening a file there and writing to it:
if (($fp = fopen($target_path, 'w')) === false) {
thrown new Exception("Failed to open file $target_path for writing");
}
fwrite($fp, file_get_contents($_FILES['image']['tmp_name']));
fclose($fp);
Chances are the problem is one of file permissions or ownership, which you'll need to fix at the command line with chmod or chown.