php rename folders with files into - php

I am trying to write a script in PHP to reset my ids in a database. But I must to rename the directories in my tables to which have same ids.
So I try to use the rename function but I failed.
I always have "No such file or directory". I try the absolute or relative path, to change chmod, to use a sleep before and yes my folders are existing.
So I would know why this doesn't work :
rename('/uploads/photos/'.$old_id.'/', '/uploads/photos/'.$new_id.'/');
And my folders have some files into.

just use linux command in php:
exec("mv $oldname $newname");

1- Fix permission of parent directory. ( photos ) It must be writable by your php user.
2- Make sure your path is ok /uploads/photos/... (Your first slash means it's an absolute path from root directory)

Related

PHP move files directory

I am trying to move my jpg image file from one directory to another using the rename() function. However it keeps on giving the error saying No such file or directory. I have changed it to the copy() function with the following error failed to open stream: No such file or directory.
I am trying this as following:
rename('/_upload/1.image.jpg', '/_accepted/test/1.image.jpg');
The file is orginally already in my htdocs/_upload folder. This PHP file is already in my htdocs folder. All permissions are set to 777 but giving the same error.
Instead of starting / in your path, use ./:
rename('./_upload/1.image.jpg', './_accepted/test/1.image.jpg');
/ means that you are giving path starting the server's root directory.
./ starts with your current directory.
In your example, the arguments to rename are file names. /_upload/1.image.jpg is an absolute file name. That means, it is relative to the root directory. It is not relative to your server root, document root or current dierctory.
try this to give complete abosolute path
rename($_SERVER['DOCUMENT_ROOT'].'/_upload/1.image.jpg', $_SERVER['DOCUMENT_ROOT'].'/_accepted/test/1.image.jpg');

PHP Unlink Not working

I am trying to delete photo in php using unlink. I have used it earlier on other server but this time it is not working. I have used absolute path for a test but still does not works:
I have used it as:
unlink('img1.jpg');
and :
unlink('http://www.mysite.com/img1.jpg');
Please anyone having such experience?
url not allow in ulink function
can you please used this
It's better, also safety wise to use an absolute path. But you can get this path dynamically.
E.g. using:
getcwd();
Depending on where your PHP script is, your variable could look like this:
$deleteImage = getcwd() . 'img1.jpg';
unlink($deleteImage);
check this
bool unlink ( string $filename [, resource $context ] )
and
filename
Path to the file.
So it only takes a string as filename.
Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.
Even though unlink() supports URLs (see here) now, http:// is not supported: http wrapper information
use a filesystem path to delete the file.
If you use unlink in a linux or unix you should also check the results of is_writable ( string $filename )
And if the function returns false, you should check the file permissions with fileperms ( string $filename ).
File permissions are usual problems on webspaces, e.g. if you upload an file per ftp with a ftp user, and the webserver is running as an different user.
If this is the problem, you have do to a
chmod o+rwd img1.jpg
or
chmod 777 img1.jpg
to grand write (and delete) permissions for other Users.
unlink($fileName); failed for me.
Then I tried using the realpath($fileName) function as unlink(realpath($fileName)); it worked.
Just posting it, in case if any one finds it useful.
php unlink
use filesystem path,
first define path like this:
define("WEB_ROOT",substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-3));
and check file is exist or not,if exist then unlink the file.
$filename=WEB_ROOT."img1.jpg";
if(file_exists($filename))
{
$img=unlink(WEB_ROOT."img1.jpg");
}
unlink won't work with unlink('http://www.mysite.com/img1.jpg');
use instead
unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg');//takes the current directory
or,
unlink($_SERVER['DOCUMENT_ROOT'].'dir_name/img1.jpg');
There may be file permission issue.please check for this.
Give relative path from the folder where images are kept to the file where you are writing script.
If file structure is like:
-your php file
-images
-1.jpg
then
unlink(images/1.jpg);
Or there may be some folder permission issue. Your files are on a server or you are running it on localhost? If it is on a server then give 755 permission to the images folder.
you are using url insted of path, here is how you should do it...
i am assuming your are uploading the picture to public_html. i suggest you to create a folder for pictures.
unlink("public_html/img1.jpg");

PHP: Cannot see upload directory

I have been struggling for a few days trying to find out how come I cannot move a file(move_uploaded_file) from temp to the directory I have setup (/img/uploads/photos).
After alot of testing and head scratching, I decided to write into CakePHP's CakeLog whatever is happening in the upload function.
Although I have physically created :/img/uploads/photos, when I use CakeLog::write('debug', 'Does directory exist?: '.json_encode(file_exists('/img/uploads/photos/'))); it logs false. Or is_dir, also returns false
Why is this happening... Can anyone help me out!
I doubt you have made a directory /img/uploads/photos/ ? It is probably inside the same dir as your other files, somewhere like /var/www/yoursite/img/uploads/photos/ or something like that.
You can use some tricks like $_SERVER{'DOCUMENT_ROOT'}, as you can see over at http://php.net/manual/en/function.file-exists.php
I don't believe you when you say you have created the /img/uploads/photos/ directory. That's an absolute path, counting from the root of the machine. It's more probable that you have created the folder in the web directory somewhere (such as /var/www/img/uploads/photos/ or /home/caboone/public_html/img/uploads/photos/).
The path /img/... means the path is relative to the disk root, i.e. it denotes the img directory in the very top level of your hard disk. I doubt that's where the directory is. Don't start the path with a / to make it relative to the file you're working in. File paths are not URLs!
Use absolute path when moving files from one directory to another.
Use dirname(__FILE__) to get the absolute current working directory, then add
the rest of the path.
Set appropriate permission to the directory, as suggested by #Alex. use chmod() and
set permission to 777 (ugo+w) [user+group+others world-writeable].

PHP/Linux Readfile Path Using Symlink

I want to move my data directory outside of the web root. I have file paths stored in my database and would like to be able to redirect looking for files through a symlink instead of having to change the saved paths.
Example, currently I have a structure like this:
/root/webroot/data/file.ext
And my database path will be: /root/webroot/data/file.ext as well. I'd like to move data outside of webroot so I have:
/root/data/file.ext
/root/webroot/data -> root/data
Where /root/webroot/data is a linux style symlink to root/data. I have set this up, but when I do a readfile on the previously stored path (/root/webroot/data/file.ext) it fails. I.e. I do this:
readfile('/root/webroot/data/file.ext');
I get back:
Warning: readfile(/root/webroot/data/file.ext) [function.readfile]: failed to open stream: No such file or directory in [file] on line ...
Is there anything I can do to help these paths resolve?
Note: The permissions of the folders and files all match. That of the symlink is root:root, from which it was created, but chown doesn't seem to work on it.
Where are you running the script from? If you are calling the directory without the leading slash:
readfile('root/webroot/data/file.ext');
then you better make sure it is running in the same directory root is sitting in. However, if you are running this from webroot, you will need to update the readfile path to something like:
readfile('data/file.ext');

how does the php copy function destination string ( ./ )work?

Okay so I'm learning about uploads and need to copy the tmp file to a specified directory so I use the copy command.
I use this inside my index.php of the main directory (learningupload/ folder)
copy($_FILES['photo']['tmp_name']['file'], './pics/'.$photoname.'.jpg');
Now I'm doing this on Xampp local host, and my uploads go to the tmp folder which is of course different from where I want the upload to go
so there's
C:\xampp\tmp
C:\xampp\htdocs\learningupload\pics\
My question is this: What is this ./ inside the destination string I need to have? Doesn't it usually mean you're moving UP a directory? Why not have it '/pics' no dot? I tried that and said it couldn't open the stream. Does the dot here mean it's referring to whatever directory index.php is in? Or am I thinking ../ moves up a directory? And single dot is different?
Thanks.
./ means "current working directory". Which directory is current working one you can see with echo getcwd();
The parent directory (the upper one) is ../
You can change your working directory using chdir() or you can just specify the full path instead.

Categories