Jump to content

[SOLVED] Upload file and create directory for each user?


sayedsohail

Recommended Posts

Hi everyone,

 

I just saw an interesting thread, for uploading files - but my problem is just to add a directory for each user, the user name are stored in session variable sess_uname?

 

I am not sure how do i check if the directory is already exists for the logged user.  if directory exists than i i just want to move the file to that directory otherwise create directory with the name as sess_uname and store the file.

 

Can anyone please help in this regard.

 

Thanks

 

<?php 

//This is the directory where images will be saved 
$path = '../images/';

//This gets all the other information from the form 
$name=$_POST['name']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']);
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);



// Connects to your Database 
mysql_connect("localhost", "root", "10000") or die(mysql_error()) ; 
mysql_select_db("docs") or die(mysql_error()) ; 

// Check the extension is valid
if (preg_match('/\.(jpg|jpeg|gif|png)$/i', $_FILES['uploadFile'. $x]['name'])){


//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$uploadFile0', '$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; 


$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);

}
else {
   // Add your error code here
   echo "Sorry, file must be of type .jpg .jpeg .gif or .png\n";
}




// check if successfully copied

if($copy){
print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
} else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop
?>

Link to comment
Share on other sites

Hi everyone,

 

I just saw an interesting thread, for uploading files - but my problem is just to add a directory for each user, the user name are stored in session variable sess_uname?

 

I am not sure how do i check if the directory is already exists for the logged user.  if directory exists than i i just want to move the file to that directory otherwise create directory with the name as sess_uname and store the file.

 

Check the manual for the following functions:

 

file_exists()

move_uploaded_file()

mkdir()

 

These should be the main ones you'll need.

 

Regards

Huggie

Link to comment
Share on other sites

This is simple code, but its not working? no errors, although i removed the session in my second try. ?

 

<?php
//session_start();
//$uname = $_SESSION['sess_uname'];
$uname = "tmp1";
$path= 'pdf/'.$uname.'/'
$filename = '/'.$path.'/doc1.doc';

if (file_exists($path)) {
echo " The Directory $path exists";
}
else {
    echo "The directory $path does not exist";
}

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?> 

Link to comment
Share on other sites

You're missing a semi-colon off of the path...

 

Try this:

 

<?php

$uname = "tmp1";
$path = 'pdf/'.$uname.'/';
$filename = '/' . $path . 'doc1.doc';

if (file_exists($path)) {
   echo " The Directory $path exists";
}
else {
   echo "The directory $path does not exist";
}

if (file_exists($filename)) {
   echo "The file $filename exists";
}
else {
   echo "The file $filename does not exist";
}
?>

 

This will noly work from your document root though as you're putting a forward slash in front of the path.  Is that what you want?

 

Regards

Huggie

Link to comment
Share on other sites

I was told using umask(0), is better before making any directories or writting files, can someone please explain little bit about umask(0), please.

 

Thanks,

 

$oldumask = umask(0);
$mode = 0777;
   if (!is_dir($path) && !mkdir($path, $mode))
       {mkdir( "$uname" ,  0777); }
umask($oldumask); 

Link to comment
Share on other sites

million thanks,

 

The directory i am creating is basically the name of the client i.e, ABC Ltd or XYZBCDD Co. or Cat's Co.,  here i wish to abstract only the alphabets and only the first bit i.e., ABC and XYZBCDD and Cats replacing the other characters exists inside the first bit as you notice Cats in place of Cat's Co.

 

I am not sure if str_replace could do all that work.

 

$file_name=$_GET['client_name'];

$file_name = str_replace("'","",$file_name);

 

or

 

substr($file_name,strpos($file_name,$pattern)+strlen($pattern));

 

or

 

$str = preg_replace('/.'*?/',  $file_name);

 

Can you please suggest something here.

Link to comment
Share on other sites

Thanks madtechie, can you please explain how it works, please. specially the /si bit is something new for me. and how do i take only the first occurance of letters from the full company ie., cat's co ltd to $file_name='cats';

 

 

$file_name="Cat's Company Ltd.";

tosomethign like this using preg_replace or preg_split, i am not sure could i achieve this.

$file_name ="cats";

Link to comment
Share on other sites

try this

**untested**

<?php
$file_name="Cat's Company Ltd.";
//replaces everything except a-z and 0-9 and spaces with nothing (the /i is case insensitive)
$file_name = preg_replace('/([^a-z0-9 ])/i', '', $file_name);
//So $file_name is "Cats Company Ltd"

//Now get the first word
if (preg_match('/(\w+)/si', $file_name, $regs))
{
$file_name = $regs[1];
}

echo $file_name;
//should be Cats
?>

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.