move_uploaded_file from sub-directory to sibling parent directory - php

I am having issues transferring an uploaded file with the class_upload library from the directory of the .php file to a sibling of the parent directory.
Here is the directory breakdown
home/hosted_domain/public_html/en_shop/profile.php <--- this is where the php file that handles the upload is
home/hosted_domain/public_html/shop/avatars/ <--- this is where the uploaded file needs to get
this is what I have tried -
move_uploaded_file($_FILES['user-avatar']['tmp_name'], "home/hosted_domain/public_html/zadomaigradinata.bg/avatars/$avatar");
I've also tried using the site's URL + the directory /avatars/ but that didnt work either.
So how should we move the uploaded file ?

Prepend the directory path with a /. This tells the system that you want that explicit path. Without it, move_uploaded_file is trying to move the file to /home/hosted_domain/public_html/en_shop/home/hosted_domain/public_html/zadomaigradinata.bg/avatars/$avatar which is not what you want.

Related

PHP auto detect upload url path

I work in responsivefilemanager and in config file I Have two line:
$upload_dir = '/user/uploads/files/'; // path from base_url to base of upload folder (with start and final /)
$current_path = '../../../../uploads/files/'; // relative path from filemanager folder to upload folder (with final /)
This wroked In xammp system (with sub folder. /user/ is sub folder). if i move in real server i need to edit this two line and remove sub folder and one ../ from two line.
Now, I need to auto detect url path. My mean is : if i install in sub folder Or root folder this two line worked in my script without manual editing.
How do can i create this?
There is no particular way to just autodetect what directory to upload to. The easiest is to solve this by just checking if the directory exists and use the other directory if it was not found.
However, I'd suggest having the same relative structure on both systems is the best solution. With the same relative structure this should work across all servers and systems without having to change anything.

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

Get correct directory path when moving uploaded files in codeigniter

I need to get the correct directory path for my codeigniter application.
I need to send all of my uploaded files in this directory:
localhost/myapplication/uploads/temp1
I have tried putting this code in my directory path for my mkdir, copy and unlink files:
$tempDirectory = base_url().'uploads/temp'.$somestring.'/';
But I realized that http addresses are not applicable when moving files. How do I move my files to the directory I want in my application?
As CodeIgniter wiki says in an example..
$config['upload_path'] = './uploads/temp1/';

Uploading a file with PHP and moving it

I have a form where users can upload images. I know how to do it with HTML/PHP and I know that I have to move the temp file to the permanent directory. Well this all works well when my file that processes the form, is in the root directory. But, I would like to have it work within another directory, on the same server.
The Structure is this:
ROOT
>images
>mobile
>upload.php
I would like to have the file put inside images, which is outside of the "mobile" directory.
My current upload path that does not work is:
define('UPLOADPATH', 'images/');
So essentially I need to go back one directory and then into images. How can I do this?
You need to add a .. To move to the parent directory. See this resource for more information

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