We help IT Professionals succeed at work.
Get Started

Full url path for uploaded file - PHP

QuintusSmit asked
Last Modified: 2012-07-04

I am working on a simple site where my student can upload files to a local hosted server.
The site runs in http://localhost/uploader.

The uploaded files reside in http://localhost/uploader/files
The problem is that when I upload the file via a form it only saves the link without the site folder as in http://localhost/files.

So the correct link to an uploaded file would be http://localhost/uploader/files/myfile.jpg
but the system save the link as http://localhost/files/myfile.jpg.

I can manually put the "uploader" folder in the saved link but want to know if there is a more elegant way to do it.

Here is the code:

if (isset($_POST['fileSubmit'])){
	$year = $_POST['year'];
	$course = $_POST['course'];
	$fileName = $_FILES['courseFile']['name'];
	$name = $_POST['name'];
	$target = UPLOADPATH.$fileName;
	//to do - use variables instead of explicit paths
	if( move_uploaded_file($_FILES['courseFile']['tmp_name'], "../files/downloads/{$_FILES['courseFile']['name']}")){
		echo "File moved to ".$target;	
		//now load xml file
		echo $name;
		echo '<br/>';
		echo $course;
		echo '<br/>';
		$xmlFile = simplexml_load_file("../files/filelist.xml");
		$newFile = $xmlFile->addChild('file');
		$newFile->addChild('title', $name);
		$newFile->addChild('year', $year);
		$newFile->addChild('course', $course);
		$newFile->addChild('link', $target);

Open in new window

Watch Question
Most Valuable Expert 2011
Author of the Year 2014
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE