Avatar of QuintusSmit
QuintusSmit

asked on 

Full url path for uploaded file - PHP

Hi

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

PHPWeb Development

Avatar of undefined
Last Comment
QuintusSmit
SOLUTION
Avatar of thesirwolf
thesirwolf

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of QuintusSmit
QuintusSmit

ASKER

Thank you both. Seems the bottom line is it has to be specified. I just wanted to check that isn't some magic PHP trick that automatically does this in a more eloquent way.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo