Save image & keep structure in PHP

Posted on 2009-04-20
Last Modified: 2013-12-08
Im currently working on a way to archive webpages using PHP. What I am currently stumped with is the a way on how I can save images from the page and keep the same folder structure.

So far I have extracted the URL to the image. What im left with is:
what I need to do now is copy that image to my server and still have the correct file structure.
for example I want it to be like this:

Any ideas?

EDIT: Maybe to make it a bit easier, maybe even a way to remove everything before the filename and the file extension. So like change '' to just head_image_cc_09.jpg. I would actually though have to be able to remove the filename and not just get image properties. Like remove all characters before and including the / symbol.

I would really really much prefer the first idea, but if thats not plausible then I guess the second way should be alright, but would require much more work.
Question by:drakeshe
    1 Comment
    LVL 5

    Accepted Solution

    I would recommend using explode("/", $remoteimage) and recursively create the missing directories using mkdir(). Then once the folder structure is in place copy($remoteimage, $localpath) would suffice to copy the file. The (untested) snippet should illustrate this.
    $remoteimage = ""
    $localpath = "/archive/"
    //Remove leading 'www.'
    if (substr($remoteimage, 0, 4) == "www.") {
    	$remoteimage = substr($remoteimage, 4);
    //Explode by slashes
    $folders = explode("/", $remoteimage);
    //Create folder structure
    for ($I=0; $I+1 < count($folders); $I++) {
    	//Add foldername to local path
    	$localpath += $folders[I] + "/";
    	if (!file_exists($localpath)) {
    		//Directory does not yet exist, create it
    //Add filename to local path
    $localpath += $folders[count($folders)-1];
    //Copy the file
    if (!copy($remoteimage, $localpath)) {
    	echo "Error copying '$remoteimage' to '$localpath'";
    } else {
    	echo "Successfully copied '$remoteimage' to '$localpath'";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now