daneth1712 Posted August 7, 2009 Share Posted August 7, 2009 Hi guys, I have a basic upload function that allows whichever files etc I choose to allow. What I would like to be able to do is.... 1. allow users to upload into a personal folder of theirs 2. set the folder to have a limit of 20MB Then... 3. Have a page that displays the users uploaded files and file size. The page I have currently is below, which is just in its basic format. <?php $file_id = ($_POST['upload']); $folder = '/path to folder/'; function upload($file_id, $folder, $types="") { $file_title = $_FILES[$file_id]['name']; //Get file extension $ext_arr = split("\.",basename($file_title)); $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension //Not really uniqe - but for all practical reasons, it is $uniqer = substr(md5(uniqid(rand(),1)),0,5); $file_name = $uniqer . '_' . $file_title; //Get Unique Name $all_types = explode(",",strtolower($types)); if($types) { if(in_array($ext,$all_types)); else { $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any. return array('',$result); } } //Where the file must be uploaded to if($folder) $folder .= '/path to folder/'; $uploadfile = $folder . $file_name; $result = ''; //Move the file from the stored location to the new location if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) { $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'";//Show error if any. if(!file_exists($folder)) { $result .= " : Folder don't exist."; } elseif(!is_writable($folder)) { $result .= " : Folder not writable."; } elseif(!is_writable($uploadfile)) { $result .= " : File not writable."; } $file_name = ''; } else { if(!$_FILES[$file_id]['size']) {//Check if the file is made @unlink($uploadfile);//Delete the Empty file $file_name = ''; $result = "Empty file found - please use a valid file."; //Show the error message } else { chmod($uploadfile,0777);//Make it universally writable. } } return array($file_name,$result);} ?> Any pointers or help anyone can offer will help me out loads. Quote Link to comment Share on other sites More sharing options...
ldougherty Posted August 7, 2009 Share Posted August 7, 2009 Since you want the uploads to go into different directories based upon the user who is uploading you would need to identify that aspect prior to uploading. If you implement a login system you could set the upload directory based upon the user who is logged in. I do not believe there is any way with PHP itself to set the max folder size as this would be a file system manipulation. You could probably do some arithmetic prior to the upload to determine if it is within the allowed number. For example get the current directory size and store it in a variable then then the difference between the current dir size and the allowed dir size is what would be available to the user and if the upload is less than that number allow it. As for having a page that shows the uploaded files and size, this can be done many different ways but the simplest would be to have an index document that simply lists the files in the directory and if they are of image type to display them as images with whatever criteria you want to associate such as file size. Hope that gives you some direction. Quote Link to comment Share on other sites More sharing options...
daneth1712 Posted August 10, 2009 Author Share Posted August 10, 2009 Hi, I have a login system, what I have now is I take the file info from the form, and use the below code to find the correct folder.... $file_id = ($_POST['upload']); $id = ($_GET['ID']); $path = '/path/to/location/'; $folder = $path . $id . '/'; I want to use the users unique 'ID' as their folder name. Is this the correct way to display the location to the folder, because I obviously need to include the '/' at the end of the full path? Quote Link to comment Share on other sites More sharing options...
daneth1712 Posted August 11, 2009 Author Share Posted August 11, 2009 Hi guys, I really need some help with this... its been too long since I used PHP and am a little outdated to say the least. I have this script below, it is supposed to store a file uploaded by a user into a personal folder which I have set as their unique ID. <?php include('../includes/sess_username.php'); include('../includes/config.php'); $db = mysql_connect("$hostname", "$adminuser", "$adminpass") or die ("Error connecting to database."); mysql_select_db("$database", $db) or die ("Couldn't select the database."); $result=mysql_query("SELECT ID FROM userinfo WHERE username='$field_id'", $db); $row = mysql_num_rows($result); if($row == 1){ while($row = mysql_fetch_array($result)){ $id=$row['ID']; $name=$_FILES['user_file']['name']; $type=$_FILES['user_file']['type']; $size=$_FILES['user_file']['size']; $time=time(); $path="uploads\\". $id . $name; $root= "http://www.domain_name.co.uk/test3/"; } } if ($size<2000000 && ereg("file", $type) ) { move_uploaded_file($_FILES['user_file']['tmp_name'],$path); $path2=$root . $path; echo "<center>"; echo "<h3>Your file Link Is: </h3><a href=$path2>$path2</a><br><br>"; echo "<br>"; echo "<img src=$path2 border='3' height='100' width='100'></img>"; echo "<center>"; $link=mysql_connect($hostname, $adminuser, $adminpass) or die("couldnt connect to database"); mysql_select_db("database") or die("Could not select database"); $query = "INSERT INTO uploads SET image = '$path2', ID = '$id'"; $result = mysql_query($query); } else { echo "you couldnt upload the same file many times per session go back and choose it or another one"; } if ($size>2000000) { echo "ERROR <br> the file size is too big<br>"; } if (!ereg("file", $type) ) { echo "ERROR <br> the file is not valid<br>"; } ?> My first question is... how can I get this to work properly? At the moment the page does nothing. I need to file to be uploaded to the following; http://www.domain_name.co.uk/test3/uploads/users_unique_ID_number/file_name Secondly, I have used 'ereg' also, which seems to now be deprecated. Can anyone help me get this script working please? Thanks in advance to anyone that can help. Quote Link to comment Share on other sites More sharing options...
daneth1712 Posted August 11, 2009 Author Share Posted August 11, 2009 Hi, Sorry to keep posting in the same topic, I have managed to rewrite part of this script, it seems to work fine, however I have stumbled at one hurdle and cant find a way to do this. The line which includes ; $path="uploads\\". $id . "\\" . $name; This wont create the folder with the value for $id. how do I make sure that a folder is created so the file can be saved to their own personal folder? This is the only part I need to get this script working. <?php include('../includes/sess_username.php'); include('../includes/config.php'); $db = mysql_connect("$hostname", "$adminuser", "$adminpass") or die ("Error connecting to database."); mysql_select_db("$database", $db) or die ("Couldn't select the database."); $result=mysql_query("SELECT ID FROM userinfo WHERE username='$field_id'", $db); $row = mysql_num_rows($result); if($row == 1){ while($row = mysql_fetch_array($result)){ $id=$row['ID']; $name=$_FILES['user_file']['name']; $type=$_FILES['user_file']['type']; $size=$_FILES['user_file']['size']; $time=time(); $path="uploads\\". $id . "\\" . $name; $root= "http://www.domain_name.co.uk/test3/"; } } if ($size<2000000) { move_uploaded_file($_FILES['user_file']['tmp_name'],$path); $path2=$root . $path; echo "<center>"; echo "<h3>Your file Link Is: </h3><a href=$path2>$path2</a><br><br>"; echo "<br>"; echo "<img src=$path2 border='3' height='100' width='100'></img>"; echo "<center>"; $link=mysql_connect($hostname, $adminuser, $adminpass) or die("couldnt connect to database"); mysql_select_db("database") or die("Could not select database"); $query = "INSERT INTO uploads SET image = '$path2', ID = '$id'"; $result = mysql_query($query); } else { echo "you couldnt upload the same file many times per session go back and choose it or another one"; } if ($size>2000000) { echo "ERROR <br> the file size is too big<br>"; } if (!query) { echo "ERROR <br> there was a problem, please try again<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
daneth1712 Posted August 11, 2009 Author Share Posted August 11, 2009 I have tried adding the following, but it doesnt seem to work, obviously I am doing something wrong, or missing something.... mkdir("$root . uploads\\ .$id",0777,true); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.