I have this structure in my public folder:
/images
/player_icons
Ajax.png
Barcelona.png
...
I need to let the admin user rename a file simply by entering a new name, rather than uploading a new file.
I have tried the old school PHP way:
rename('/images/player_icons/Ajax.png', '/images/player_icons/test.png');
This produces the error:
File not found at path: images/player_icons/Ajax.png
I have also tried using the Storage facade, although from the looks of it, this can't work in the public folder:
Storage::move('/images/player_icons/Ajax.png', '/images/player_icons/test.png');
I don't understand why the rename() option doesn't work. If I visit example.com/images/player_icons/Ajax.png in the browser, the image is there.
How do I make this happen?
you may need to give the public_path helper function a try
The public_path function returns the fully qualified path to the
public directory. You may also use the public_path function to
generate a fully qualified path to a given file within the public
directory
However, renaming things on the public directory directly means that you HAVE to give this directory a write permissions or change the ownership for this directory to be owned by the server user ( which is almost will be www-data )
this is kind of a risky thing.
The better approach is to ( symlink ) the storage path ( which is writable by default ) and let the user upload and rename files there
more about this will be found here : File system docs
Use this
rename(public_path('/images/player_icons/Ajax.png'), public_path('/images/player_icons/test.png'));
Related
I started writing an app for nextcloud. For my app to work, I need to be able to pass the complete path of files located in the file directory of the logged in user to a CLI command.
I know that the base path of the file directory is defined in config.php; for example, it is 'var/www/html/nextcloud' or 'var/www/nextcloud'.
I have found a couple of functions that allow me to get the relative path, e.g.
\OC\Files\Filesystem::getFileInfo($path)
and
\OC\Files\Filesystem::getInternalPath($path)
Unfortunately, I couldn't find a function that either directly returns the full path of a file or at least the base path from config.php.
Do any of you have a tip for me?
Thanks for your help, misorude!
In the meantime I have found a solution:
in Nextcloud there is a class \OC\Config which has the method OC\Config::getValue($key, $default=null)
(see https://docs.nextcloud.com/server/latest/developer_manual/api/OC/Config.html)
The difficulty I had with this was that the initialization of a class instance expects the specification of a path, namely the path where the config.php file is located.
This irritated me at first, because it seems you are chasing your own tail here.
Then I tried specifying only the relative path to the Nextcloud installation, and that worked immediately. So to make it short, here's the code you need:
$config = new \OC\Config('config/');
$base_path = $config->getValue('datadirectory')
datadirectory is the key in the array defined in config.php that contains the base directory.
$basepath now contains a path like /var/www/html/nextcloud/data.
I hope this helps someone.
Best regards,
Tom
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);
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");
I am writing installation script in PHP and part of my application may be installed in the folder that is one level above the public folder. Usually, public folder is called "public_html" but that may not always be the case. Is there some easy way to locate the name or path of the root public folder using PHP?
Try $_SERVER['DOCUMENT_ROOT']. (documentation)
If you want to get the directory above it, you can use dirname.
Brad does have a point, though; your script may not have access to that directory.
$_SERVER['DOCUMENT_ROOT'] will provide the full path up to public_html and you can explode with slash "/" and get last index of the exploded array.
You can also use realpath with argument "../", if you don't know the actual file name. This will return you the full parent path of public_html.
Hope one of this two will work for you :)
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.