开发者

Can I attach data gathered by a form to a file that is being uploaded?

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>";
    }


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.


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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜