Renaming a file results in 'No such file or directory' - php

I've been trying to rename an image file using php using this code.
$user_id = $_POST[user_id];
$old_image_name = $_POST[old_image_name];
$image_name = $_POST[image_name];
rename('/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'', '/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'');
The error I'm getting is-
"Warning: rename(/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg,/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg) [function.rename]: No such file or directory in /var/sites/o/oddmeme.com/public_html/_process/post_single_meme_edit.php on line 8"
The image is definitely there. I've tried removing the / at the start and trying a few different version but nothing has worked.
I've also set permission to 777 so that shouldn't be a problem.

First of all, you have an error in your code:
You are using twice the same path in your function with $old_image_name...
Fix that and tell us if it's working.

Related

Php Problem with move_uploaded_file. It seems everything is fine but it is not working

I am trying to use move_uploaded_file but I think everything is fine but my code is not working. Everything is working as expected even image name is being inserted into DB but it is not moving into uploads folder. Folder is there and apache2 does have access to write it.
I don't have enough reputation to post more then 8 lines so everything is on pastebin Here. The main aprt is...
$post_image_new_name = uniqid('', true). "."
.$post_image_actual_ext;
$post_image_destination = '../../uploads/';
if (is_dir($post_image_destination) && is_writable($post_image_destination)) {
$post_image_destination = '../../uploads/'.$post_image_new_name;
var_dump($post_image_destination);
move_uploaded_file($post_image_new_name, $post_image_destination);
echo "Inside move_uploaded_file section";
https://pastebin.com/Z321R76z
Try this code
line 53: move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);
The first param should be the file source name.
move_uploaded_file() expects the source filename of the uploaded file. So you must use something like this:
move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);

Receiving unlink() error after it has been successful

this might be a really stupid question but I am getting the following error after the code has successfully deleted the file and I can not work out why, the code is very simple it gets the name and path of the file to be deleted from the database and then deletes it.
Code:
$getFiles = mysql_query("SELECT * FROM tempFiles WHERE pTID='$passedId'");
$numFiles = mysql_num_rows($getFiles);
for ($f=0;$f<$numFiles;$f++) {
$fileName = mysql_result($getFiles,$f,"fileName");
$deleteFile = "../../".$fileName;
unlink($deleteFile);
}
Warning: unlink(../../files/projects/files/643115.jpg): No such file or directory
The script for deleting the file is in a scripts/php/thefile and the file is in files/projects/files/thefile, so the ../../ is definitely needed and not the issue as far as I can tell. I know that the file is being deleted successfully because it is no longer in the folder after I run the script so I have no idea what is causing the error.
Any ideas why I might be getting the error?
Thank you in advance.
Possible causes to the error:
There are more than 1 record in the tempFiles table with the same fileName, so the first attempt removes it and the second causes the error.
The file didn't exists on the folder when you ran the script (as #AxelAmthor said on comment)
To solve it, just add a verification (as #Sammitch said on comment):
if (is_file($deleteFile)) {
unlink($deleteFile);
}

file_get_contents() failed to open stream: No such file or directory

I am writing some php script to update the code on my sites. In order to do that I have written the following lines which checks for the update version and bring the name from where I use to distribute my updates ans then creates the link of that name. I have done something like this.
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
$contents = file_get_contents($filename);
echo $contents;
I am getting this error
failed to open stream: No such file or directory.
Even though the file is present still I am getting the same error. I have turned on my allow_url_fopen to on. The above code is working from my local host but not from the server. I have to update a major bug fix and I am stuck.. Please someone help me soon..
remove extra .php from url
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
You will get error if you are doing this way...
$filename = "marynyhof.com/Info.php"; // will give you error, which exactly you are getting
use full url like this
$filename = "http://www.marynyhof.com/Info.php"; //will work

PHP - Save image uploaded with AJAX

Given this one a few hours research and thought but I'm not getting anywhere.
I have managed to get an image file to upload through AJAX using a FormData object. When the file reaches the php code I am able to access its info such as 'name' and 'type' by using:
$_FILES['newFile']['name'];
$_FILES['newFile']['type'];
And so on, which means that the file must be uploading as intended, I just can't seem to save it to a file from there.
I have tried:
$file = file_get_contents($_FILES["newFile"]['tmp_name']);
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
But then imagejpeg gives me the error "Expects paramater1 to be resource, string given."
So I tried:
$file = imagecreatefromstring(file_get_contents($_FILES["newFile"]['tmp_name']));
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
I now receive the error: /"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."
Can someone explain how I get the image from the array to a file on disk?
You have to use the move_uploaded_file function like this:
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $pathfilename)
Use php function move_uploaded_file("$_FILES['newFile']['tmp_name']","Location_where_you_want_to_save");
Figured it out! Obviously "/img/uploads/" was the issue. PHP doesn't recognize a forward slash as the root directory! My fault!
move_uploaded_file($_FILES["newFile"]['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/uploads/' . $_FILES["newFile"]['name']);
Using $_SERVER['DOCUMENT_ROOT'] before the new file path worked perfectly.

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

Categories