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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.