?
Solved

Save image & keep structure in PHP

Posted on 2009-04-20
1
Medium Priority
?
344 Views
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:
www.testsite.com/img/main/layout/head_image_cc_09.jpg
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:
copy testsite.com/img/main/layout/head_image_cc_09.jpg
to www.mysite.com/archive/testsite.com/img/main/layout/head_image_cc_09.jpg

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 'testsite.com/img/main/layout/head_image_cc_09.jpg' 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.
0
Comment
Question by:drakeshe
1 Comment
 
LVL 5

Accepted Solution

by:
AtanAsfaloth earned 2000 total points
ID: 24184305
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 = "www.testsite.com/img/main/layout/head_image_cc_09.jpg"
$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
		mkdir($localpath);
	}
}
 
//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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

850 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