views:

43

answers:

4

I need customers to upload files to my website and I want to gather their name or company name and attach it to the file name or create a folder on the server with that as the name so we can keep the files organized. Using PHP to upload file

PHP:>>

if(isset($_POST['submit'])){
    $target = "upload/";
    $file_name = $_FILES['file']['name'];
    $tmp_dir = $_FILES ['file']['tmp_name'];

    try{
    if(!preg_match('/(jpe?g|psd|ai|eps|zip|rar|tif?f|pdf)$/i', $file_name))
        {
        throw new Exception("Wrong File Type");
        exit;
        }
        move_uploaded_file($tmp_dir, $target . $file_name);
        $status = true;
        }

    catch (Exception $e)
        {
        $fail = true;
        }
}

Other PHPw/form:>>

<form enctype="multipart/form-data" action="" method="post">
        input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />
            label for="file">Choose File to Upload </label> <br />input name="file" type="file" id="file" size="50" maxlength="50" /><br />
            input type="submit" name="submit" value="Upload" />

php
    if(isset($status)) {
        $yay = "alert-success";
    echo "<div class=\"$yay\">
    <br/>
    <h2>Thank You!</h2>
    <p>File Upload Successful!</p></div>";
    }
    if(isset($fail)) {
        $boo = "alert-error";
    echo "<div class=\"$boo\">
    <br/>
    <h2>Sorry...</h2>
    <p>There was a problem uploading the file.</p><br/><p>Please make sure that you are trying to upload a file that is less than 50mb and an acceptable file type.</p></div>";
    }
+1  A: 

Look at mkdir(), assuming the user PHP is running as has appropriate permissions, you can simply make a directory inside of uploads/.

After that, you can modify $file_name to contain some of the other posted variables that you mentioned you will add. Just take care to ensure those variables contain only expected characters.

Tim Post
A: 

Do your customers need to Log into your site before they upload? If that's the case perhaps you can store/grab $_SESSION information regarding their company and name. You could then append that info to the $file_name or the $target directory.

the mkdir() idea below this looks like it will probably work, but have you considered what would happen if a user entered someone else's name, had someone else's name, or entered someone else's company?

no there is no login, can the mkdir() grab form data and use that as a name? If the customer uses another name, then it's their loss. They have no reason to do that, they will only be uploading art files for printing.
Jacob
A: 

use this code on the top of the page

$path = dirname( __FILE__ );
$slash = '/';

(stristr( $path, $slash )) ? '' : $slash = '\\';
define( 'BASE_DIR', $path . $slash );

& use below code after inside if below exit;}

$folder  = $file_name;            // folder name
$dirPath = BASE_DIR . $folder;   // folder path

$target = @mkdir( $dirPath, 0777 ); 

move_uploaded_file($tmp_dir, $target . $file_name);

here is your code as it is

diEcho
A: 

I figured it out, I just made a input for the name ant attached it to the file name.

Jacob
please select one right answer, this will help in future, otherwise people not reply your question bcoz u will not have any accepting %
diEcho