:-
Hello all. I’ve created a two-form page, and could use some advice on: either how to go about what I want, or else what’s a much easier way to do the following. I know that I could build a separate page for each function, but was hoping to keep it consolidated.
The first form on the page has a basic text input that creates an upload directory ($DirName) upon submitting (‘create’,$_POST).
The second form uploads photos to the newly named directory upon submitting (‘upload’,$_POST).
Both work individually, but I can’t seem to pass the new directory name to the upload form.
[ol][li]I’ve tried passing the variable through $_REQUEST. Works the same as $_POST, and disappears once 2nd $_POST script runs[/li]
[li]using $_GET in the first form successfully passes it along and even uploads photos. But, consequently it ALSO passes the mkdir() request to the first form again, generating a “File already Exists” error message.[/li][/ol]
Here’s what I’ve got:
[php]
define(‘UPLOAD_DIR’,’/beaten/path/’);
/---------------------BEGIN STEP 1. CREATE DESTINATION DIRECTORY -------------/
//mundane stuff has been left out
if (array_key_exists(‘create’,$_POST) && !empty ($POST[‘directoryName’])) {
$DirName = str_replace(’ ‘,’’,$_POST[‘directoryName’]);
if(!is_dir(UPLOAD_DIR.$DirName)) {
$dir_success = mkdir(UPLOAD_DIR.$DirName);
}
else {
$create_warning = “$DirName already exists at this location. Either select it from the drop-down menu to add more photos, or give the new folder a unique name.”;
}
}//end if ‘creat’,$_POST
/----------------BEGIN STEP 2. UPLOAD FILES ----------------/
if (isset($DirName) && array_key_exists(‘upload’,$_POST)) {
//size & type check omitted for easy reading
if($sizeOK && $typeOK) {
switch($_FILES[‘image’][‘error’]) {
case 0:
//move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES[‘image’][‘tmp_name’], UPLOAD_DIR.$DirName.’/’.time().$file);
if($success) {
$result = “”$file" has been uploaded successfully to $DirName.";
}// end if $success
else {
$result = “Error uploading “$file”. Please try again.”;
} //end else
break;
case 3:
$result = “Error uploading “$file”. Please try again.”;
default:
$result = “Sustem error uploading “$file”. Contact webmaster.”;
}//end switch
}//end if $sizeOK
elseif ($_FILES[‘image’][‘error’]==4) {
$result = “No file selected”;
}//end elseif
else {
$result = “”$file" cannot be uploaded.
- Maximum size: 1MB (1024KB)
- JPEG (jpeg, jpg, JPG) only
}//end else
}//end if ‘upload’,$_POST
if (array_key_exists(‘upload’,$_POST) && !isset($DirName)) {
$result= “There was a problem.”; //I KEEP GETTING THIS ERROR UPON (‘upload’,$_POST)
}
?>
[/php]
Image Folder Name:
<?php
// if uploaded, display $result
if (isset($result)) {
echo "<div id=\"success\"><p>$result</p></div>";
}
?>
Step 2: Upload Photos
Upload photo: *" />