Jump to content

File Upload


zipp

Recommended Posts

I am trying to have a file uploaded into a directory, based on user input. If the folder they are uploading to doesn't exist, it is created. The only thing I can think of, is that this method of file uploading will only work to 1 folder deep.  That is only a guess though. Also the "destination" will only be 1 word long.

 

<?php

//find out if the user specified a target location, other then default.
if (!$_REQUEST['destination']){
$destination = "";
} else {
$destination = $_REQUEST['destination'] . "/";
}

//where the file is getting uploaded
$target = "upload/" . $destination;
$target = $target . basename( $_FILES['uploaded']['name']) ;

//check to see if file exists
if (!file_exists($target)) {
    mkdir($target, 0777);
}

//upload file
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo '<html><head><meta http-equiv="refresh" content="5;url=index.php"></head><body>';
echo "The file ".
basename( $_FILES['uploadedfile']['name']). " has been uploaded<br />";
echo "You will be taken back to the admin page in 5 seconds.";

}else{

echo "Sorry, there was a problem uploading your file.<br />";
//echo $target;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/62171-file-upload/
Share on other sites

This function will allow you to create directories infinitely deep:

<?php
function mkdir_recursive($folder)
{
$folders = explode('/',str_replace('\\','/',$folder));

$temp_folder = null;
for($i=0; $i<count($folders); $i++)
{
	$temp_folder .= $folders[$i].'/';
	if(!is_dir($temp_folder) && !file_exists($temp_folder))
	{
		mkdir($temp_folder);
	}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/62171-file-upload/#findComment-309536
Share on other sites

I have been stuck on this for a while now, its really starting to wear me down. Here is the newest code that I am working with.

 

index.html

<form enctype='multipart/form-data' action='upload.php' method='POST' id='form'>
Please choose a file: <input name='up' type='file' /><br />
User to upload file to: <select  name='destination'>
<option value="jackie">jackie</option>
<option value="test">test</option>
<option value="tom">tom</option>
</select><input type='submit' value='Upload' />
</form>

 

upload.php

<?php

//check destination to see if its different then default
if (!$_REQUEST['destination']){
$destination = "";
} else {
$destination = $_REQUEST['destination'] . "/";
}

//check upload directory
chmod("upload", 0777);

//where the file is getting uploaded
$target = "upload/" . $destination;
$target = $target . basename( $_FILES['up']['name']) ;

//make sure target tirectory exists
if (!file_exists($target)) {
    mkdir($target, 0777);
}

if(move_uploaded_file($_FILES['up']['tmp_name'], $target))
{
//worked
echo '<html><head><meta http-equiv="refresh" content="5;url=index.php"></head><body>';
echo "The file ".
basename( $_FILES['uploadfile']['name']). " has been uploaded<br />";
echo "You will be taken back to the admin page in 5 seconds.";

}else{
//error
echo "Sorry, there was a problem uploading your file.<br />";
echo $_REQUEST['up'];
echo $target;
print_r($_FILES);
}
?>

 

this will print out:

Sorry, there was a problem uploading your file.

upload/test/Array ( )

Link to comment
https://forums.phpfreaks.com/topic/62171-file-upload/#findComment-309930
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.