Jump to content

multiple upload and target folders help


lucan

Recommended Posts

Dear all PHP experts need your help please as I'm very new the PHP. I found this php form script on the net which works perfectly but only uploads one file. I need to upload a second file and send it to a different folders and a different field on the database. I have viewed a number of site and changed the code a number of time without success I have put the code back to original script, can some one please show me what I need to do. Thanks

 

<form action="upload.php" method="post" enctype="multipart/form-data"> 

              <label>First Name</label><input type="text" name="fname" /><br /> 

              <label>Last Name</label><input type="text" name="lname" /><br />       

              <label>Upload Image</label> <input type="file" name="image" /><br>

              <label>Spec</label> <input type="file" name="spec" /><br />

              <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 

              <input type="submit" id="submit" value="Upload" /> 

</form> 

 

 

Script

 

      function is_valid_type($file) 

{   

    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); 

    if (in_array($file['type'], $valid_types)) 

        return 1; 

    return 0; 

function showContents($array) 

    echo "<pre>"; 

    print_r($array); 

    echo "</pre>"; 

$TARGET_PATH = "images/"; 

 

$fname = $_POST['fname']; 

$lname = $_POST['lname']; 

$image = $_FILES['image'];

 

$fname = mysql_real_escape_string($fname); 

$lname = mysql_real_escape_string($lname); 

$image['name'] = mysql_real_escape_string($image['name']);

 

$TARGET_PATH .= $image['name']; 

 

if ( $fname == "" || $lname == "" || $image['name'] == "" ) 

    $_SESSION['error'] = "All fields are required"; 

    header("Location: index.php"); 

    exit; 

if (!is_valid_type($image)) 

    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; 

    header("Location: index.php"); 

    exit; 

if (file_exists($TARGET_PATH)) 

    $_SESSION['error'] = "A file with that name already exists"; 

    header("Location: index.php"); 

    exit; 

if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) 

    $sql = "insert into people (fname, lname, filename,spec) values ('$fname', '$lname', '" . $image['name'] ."')"; 

    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); 

    header("Location: images.php"); 

    exit; 

else

{   

    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory"; 

    header("Location: index.php"); 

    exit; 

Link to comment
https://forums.phpfreaks.com/topic/228451-multiple-upload-and-target-folders-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.