cozmir Posted November 26, 2007 Share Posted November 26, 2007 Hi, I am using the existing upload script and would like to add the ability to have the files drop into a directory based on the name chosen from the drop down list on the form. Currently all files drop into the same directory all the user is notified via email. I want to keep the email notification ability. I'm new to PHP and any help is greatly appreciated. Thanks! Uploader Script... <?php session_start(); if(isset($_POST['_submit'])) { $files = array(); $errors = array(); foreach($_FILES as $f) { if($f['error'] == UPLOAD_ERR_OK) { $ts = time(); $filename = $ts . '-' . preg_replace('/[^a-zA-Z0-9_\.-]/', '', $f['name']); $ok = move_uploaded_file($f['tmp_name'], '/upload/files/' . $filename); chmod('/upload/files/' . $filename, 0644); $files[] = $filename; } else { switch($f['error']) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $errors[] = $f['name'] . ' could not be uploaded because the filesize is too large.'; break; case UPLOAD_ERR_PARTIAL: $errors[] = $f['name'] . ' could not be uploaded because the upload could not be completed.'; break; } } } foreach($_POST as $k => $v) { $$k = stripslashes($v); } $file_paths = ''; foreach($files as $file) { $file_paths .= "$file\n"; } $mail_to = array_pop(explode(' - ', $notify)); $message .= "Company Name: $company_name\n"; $message .= "Name: $name\n"; $message .= "Contact Phone: $contact_phone\n"; $message .= "Contact Email: $contact_email\n"; $message .= "Name of Advertiser: $name_of_advertiser\n"; $message .= "Comments: $comments\n\n"; $message .= "Files:\n" . $file_paths . "\n"; $ok = mail($mail_to, "File Upload", $message); if($ok && empty($errors)) { $_SESSION['msg'] = 'Your files were successfully uploaded.'; } else { $_SESSION['msg'] = implode('<br />', $errors); } header('Location: upload_confirm.php'); exit; } else { header('Location: index.php'); exit; } ?> Form Code... <form action="upload_process.php" method="post" enctype="multipart/form-data" onSubmit="return validate_form(this);" /> <table align="center"> <tr> <td align="right" valign="top"><p>Your Company</p></td> <td valign="top"><input type="text" size="55" name="company_name" value="" /></td> </tr> <tr> <td align="right" valign="top"><p>Name</p></td> <td valign="top"><input type="text" size="55" name="name" value="" /></td> </tr> <tr> <td align="right" valign="top"><p>Phone</p></td> <td valign="top"><input type="text" size="55" name="contact_phone" value="" /></td> </tr> <tr> <td align="right" valign="top"><p>Email</p></td> <td valign="top"><input type="text" size="55" name="contact_email" value="" /></td> </tr> <tr> <td align="right" valign="top"><p>Advertiser</p></td> <td valign="top"><input type="text" size="55" name="name_of_advertiser" value="" /></td> </tr> <tr> <td align="right" valign="top"><p>Please Notify</p></td> <td valign="top"> <select name="notify"> <option value=''></option> <option value="test - test@testing.com">Testing</option> <option value="test2 - test2@testing.com">Testing 2</option> <option value="test2 - test2@testing.com">Testing 3</option> </select> </td> </tr> <tr> <td align="right" valign="top"><p>File Uploads</p></td> <td valign="top"><br /> <p>File 1:<input type="file" name="file1" /></p> <br /> <p>File 2:<input type="file" name="file2" /></p> <br /> <p>File 3:<input type="file" name="file3" /></p> <br /> </td> </tr> <tr> <td align="right" valign="top"><p>Comments</p></td> <td><textarea name="comments" rows="10" cols="40"></textarea></td> </tr> <tr> <td> </td> <td><input type="submit" id="_submit" name="_submit" value="Upload" /></td> </tr> </form> </table><br/><br/> Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 26, 2007 Share Posted November 26, 2007 Method one rename("FILECURRENTDIRECTORYHERE/file.txt", "THEFILESNEWDIRECTORYHERE"); So at the end of the upload into whatever directory ur currently uploading into use that as the first parameter and then the value from dropdown menu as the second with the filename on the end of that. Method two move_uploaded_file ( string $filename , string $destination ) That will a) check to see if the file is correct from uploading via the post mechanism and then the destination is where you want it to be moved too. $filename as the temporary filename given by the upload process.You already have this in your code you just need to change part of it to send it to the path you require via the dropdown menu Remember to make sure your folderss have the right CHMOD or (permissions to be written to) Quote Link to comment Share on other sites More sharing options...
cozmir Posted November 26, 2007 Author Share Posted November 26, 2007 Thanks Distant_storm - I guess what I could not figure out is if I use Method Two how do I set the destination directory to change based on the results of the drop-down selector on the form? Method One totally lost me! Sorry, I'm new to this! Quote Link to comment Share on other sites More sharing options...
cozmir Posted November 26, 2007 Author Share Posted November 26, 2007 Whoah, went back and spent some more time with Method One and it worked! Thanks! Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 27, 2007 Share Posted November 27, 2007 gd gd glad it worked for you mate. Although method two tends to be best but if you prefer the rename method thats ok. 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.