PHP move files directory - php

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');

Related

passing root path in move_uploaded_file

i have a constant variable named SADMIN
define('SADMIN','http://localhost/synthesis_study_material/student_admin/');
and i am trying to use SADMIN constant in my move_uploaded_file function
move_uploaded_file($_FILES['name']['tmp_name'], SADMIN."include/uploaded/epub/".$_POST['name']);
my current file is in
http://localhost/synthesis_study_material/synthesis_notes_admin/index.php
file is not getting uploaded in expected folder and move_uploaded_file is not giving any warning or error
The destination of move_uploaded_file needs to be a file path, not a url. To clarify this your webserver has a specified folder on the file system set as the web root. This might be something like '/var/www'. This means when you go to 'http://localhost/' it will look for a file such as '/var/www/index.php' or '/var/www/index.php'.
the destination might be an absolute path or relative, so you might supply 'synthesis_study_material/student_admin/' and that might be the same as '/var/www/synthesis_study_material/student_admin/' (depending on your web root and where move_uploaded_file is called from). Note you can use dirname(__FILE__) to get the path of the current php file. Also note that move_uploaded_file will not create directories for you if they don't exist.
I suggest starting with a simple example such as move_uploaded_file($_FILES['name']['tmp_name'], 'test-upload.txt'); to see if anything gets uploaded, and make modifications from there.

Cannot find a file available in the same folder

I'm trying to read the content of config.json file which is available in the same folder of the file that read it, this file is called settings.php, I actually have this line of code in settings.php:
$jsonData = file('config.json');
the problem's that I get:
Warning: file(config.json): failed to open stream: No such file or directory
this is weird because as you can see from the image:
I'm trying this app on localhost with mamp
This is most likely because your settings.php is included by a index.php that's a directory higher.
Php file opening will always assume from the "parent" php file where execution started as base directory and not of the included files.
Try using __DIR__
$jsonData = file(__DIR__.'/config.json');
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory.

php rename folders with files into

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)

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