Creating directories with regex same in url structure - php

I will mass download thousands of images from a server.
My problem is : filenames are same and they are located in different directories.
Ex:
http://domain.com/images/upload/2014/09/SKU00123/1.jpg
http://domain.com/images/upload/2014/09/SKU1501/1.jpg
I want to download them with the same directory structure.
c:\images\upload\2014\09\SKU00123\1.jpg
I can take the file name with basename command but i couldn't find a way to get the directory structure. I need php to create directories and save the files to that destination.
Is there a way to change the url structure to directory structure? Maybe with regex?

For the next time, please show us some PHP code. Have you already tried something?!
...You can easily do this in 2 steps:
Use parse_url to find the path(/images/upload/2014/etc..) of the URL.
Use mkdir with the recursive parameter to create these directories on your own system.

Related

How to move a folder to another folder PHP? [duplicate]

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.
Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");
There's a specific PHP function for it
http://php.net/manual/en/function.rename.php
If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]
I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

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.

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

How to move one directory to another directory?

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.
Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");
There's a specific PHP function for it
http://php.net/manual/en/function.rename.php
If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]
I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

Creating a file/folder structure and zipping it up?

I have a directory of image files and I need a php script or shell script that will rename them, create a structure of nested directories, and then insert each image into a specified place in the directory hierarchy. Ideally I would just specify a parent directory for each file and a parent directory for each directory and it would build it. And then finally, I need the script to zip up the whole thing.
There's probably not an existing php class that will do all this for me, but if anyone knows of a php class or other script available online that would handle a lot of this logic that would be great.
I know its not PHP, but you might want to investigate something like this:
a shell script for renaming files (your dialect may vary, but the man pages are very helpful).
foreach img (/path/to/directory/*.jpg)
set newimg= `echo $img | sed 's,path/to/directory/(.+)\.jpg,new/path/$1newname.jpg/,'`
cp img newimg
end
If all the files in a particular directory are going to one location, something like the above might work. Essentially it loops through the target directory getting the names of the files that have a .jpg (or whatever) extension. Then it takes those names, including their path and subs in the new directory path and some change to the original file change. I've used , for the separator in the substitution because escaping all those path separators is a pain. Lastly, it copies the old file to the new location.
Since your directory needs are probably more complex than a hardcoded path allows for, you can include a line to parse your filepath/filename and determine what its target path should be; and use that in the substitution.
A snippet for creating a directory tree in one go can be found here.
You may also decide that find is a better fit for this than foreach because it can descend a directory structure as far as you like.

Categories