How do I copy files repository to new folder with PHP - php

I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.
copy(file,dest) does not work.
rename(file,dest) moves the files to the new folder but I lose them in the repository.
How do I copy the files in one folder to the new folder without losing the files in the original folder?
$dest = '../'.$menuLocation.'/'.$pageName;
$file1= "repository/index.html";
$file2= "repository/content.php";
mkdir($dest,0777);
rename($file1,$dest.'/index.html');
rename($file2,$dest.'/content.php');
$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.

For anyone hunting for the solution to this:
when using copy() in php you also need to include the file name.
copy(orginalfile,destination_with_filename);
for example:
wrong:
copy('/temp/sports/basketball.jpg','/images/sports/')
Correct:
copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')

Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.

Related

How to Make a New Subdirectory Under A Specified Parent Directory

I'm sure this is an easy fix but I'm new to PHP and have been stuck on this for a while.
My application creates a new folder for each 'user.' My issue is that the new folder just saves directly to the root directory of the project. I would like the data to save to a 'data' folder within the root.
mkdir($user_id); //creates a folder with the user's name at root directory
mkdir("/path/to/project/data/" . $user_id) //should create a new folder under a specified directory?
What am I missing? Shouldn't the second code create the folder in the specified directory? Both lines of the code seem to create username folders at the root directory when run.
This is mostly comment, but a bit long to fit in the available space.
mkdir($user_id); //creates a folder with the user's name at root directory
No it doesn't. Assuming that the string passed as an argument does not contain a directory seperator, it creates a directory in the current directory where your code is running. If your code is running in the root directory, your security model is very, very broken.
mkdir("/path/to/project/data/" . $user_id) //should create a new folder under a specified directory?
Only if /path/to/project/data already exists, is writeable and is a directory. Unless $user_id contains "../../../../.." I think its very unlikely that this will create a directory in /.
Both lines of the code seem to create username folders at the root directory when run.
You didn't say if your application is accessed via a webserver or is running natively on the system via CLI or as a daemon. Nor did you mention what OS this is on.
If your webserver can modify your root directory then your security is seriously broken and MUST be fixed. If your code is running in the CLI API, then you shouldn't be running it as root / and admin user.

Including a folder and accessing images in the folder

I am trying to access to a folder using "include $path" inside a php file.
Inside the folder, it includes a lot of images, and in php, it includes many tags of img that accesses to the images inside the folder.
Is there any way to access to a image's path inside folder using "include"?
That is not what include is for.
Include let you include another piece of PHP (some class, some functions, whatever), it is not for 'accessing' some folder.
If you want to see the contents (images) of that folder, try scandir:
https://secure.php.net/manual/en/function.scandir.php
Also make sure your webserver has rights to read those files.
On *nix/apache this user is often named www-data (I have seen 'nobody' and 'apache' too as usernames). On IIS it is typically something like IUSR.

Laravel - Using allfiles gives back empty array

I've been using laravel for nearly all my projects and I've came across a problem when trying to list all files in a directory.
Here is my code below:
$directory = ("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
So as you can see First line is the directory which is in Upload followed by the username of the account.
The second line is meant to scan this directory and list only files. Sadly it always gives me back an empty array. Using Scandir() here works... But it gives me folders which I'm not wanting.
So... Any tips? I checked the API and I really can't see what is wrong here :(
The reason you're getting no results when using the Storage::allFiles('directory') method is this method only looks inside of your application's storage folder, usually storage/app. Hence why modifying the storage path in configuration works as mentioned by #yassine (you shouldn't do this).
If you wish to use files outside of the storage folder, simply use the File facade instead. e.g. File::allFiles('directory')
Me too i had the same issue for local storage. I fixed it by :
project/config/filesystems.php
line 44
changed root folder to from storage_path('app') to changed to
base_path()
then I provided a related path to the root folder in Storage::files() or Storage::allfiles().
Like Storage::allfiles("app") if I want to fetch files withing the sub folder "app" relative to the base path given in the config file.
Good luck
$direcotry has to be an absolute path from your laravel root folder.
When your uploaded folder is in the root folder try this
$directory = base_path("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);

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

Without using PHP copy function, move file from one folder to another

How can we move the one file from one folder to another folder without copy function in PHP.
In php copy function used to copy the file from one folder to another folder. But in my case I don't use copy function for move the file from folder to another folder.
You can try and make use of php rename function. It will move the file if a new directory was specified in the new move to filename. Just ensure that the permission are correct when moving and creating new folders and files.
rename("path1/file.txt", "path2/file.txt");
PHP RENAME
Attempts to rename oldname to newname, moving it between directories
if necessary. If newname exists, it will be overwritten.
If you do not want use PHP copy() function for some reason, you can do this like:
system('cp /path/to/src_file /path/to/dst_file');
for *nix systems, and
system('copy /path/to/src_file /path/to/dst_file');
for Windows
Suggest using rename().
http://php.net/manual/en/function.rename.php
rename('/dir1/old_dir/old_name.txt','/dir1/new_dir/zzz_name.bak');

Categories