PHP Form help

Hi guys,

I’ve been writing a simple form with user input and file upload abilities.

Everything was working okay… kind of. I can capture all of the users info, file uploading is working for the image field but not the CV field. Also, when images are saved on the server, I cannot open them?

Here is my form:

http://www.soft-links.co.uk/

and here is my PHP code (please note I’m no expert, my code may be messy).

Why can I not capture both the image and CV? Also, is it possible to add checks for type etc?

[php] <?php

ini_set(‘display_errors’,1);
error_reporting(E_ALL);

?>

<?php include ('connect.php'); //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This is the directory where images will be saved $target2 = "cv/"; $target2 = $target2 . basename( $_FILES['cv']['name']); // Get values from form $first_name = $_POST['first_name']; $surname = $_POST['surname']; $address_line_1 = $_POST['address_line_1']; $address_line_2 = $_POST['address_line_2']; $city = $_POST['city']; $post_code = $_POST['post_code']; $country = $_POST['country']; $date_of_birth = $_POST['date_of_birth']; $interests = $_POST['interests']; $height = $_POST['height']; $chest_size = $_POST['chest_size']; $waist_size = $_POST['waist_size']; $inside_leg_size = $_POST['inside_leg_size']; $outside_leg_size = $_POST['outside_leg_size']; $shoe_size = $_POST['shoe_size']; $female_dress_size = $_POST['female_dress_size']; $passport = $_POST['passport']; $driving_license = $_POST['driving_license']; $pic = ($_FILES['photo']['name']); $cv = ($_FILES['cv']['name']); // Insert data into mysql $sql="INSERT INTO $tbl_name(first_name, surname, address_line_1, address_line_2, city, post_code, country, date_of_birth, interests, height, chest_size, waist_size, inside_leg_size, outside_leg_size, shoe_size, female_dress_size, passport, driving_license, photo, cv)VALUES('$first_name', '$surname', '$address_line_1', '$address_line_2', '$city', '$post_code', '$country', '$date_of_birth', '$interests', '$height', '$chest_size', '$waist_size', '$inside_leg_size', '$outside_leg_size', '$shoe_size', '$female_dress_size', '$passport', '$driving_license', '$pic', '$cv')"; $result=mysql_query($sql); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your photo."; } // ---------------------------- // //Writes users cv to the server if(move_uploaded_file($_FILES['cv']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['cv']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your cv."; } // ---------------------------- // // if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "
"; echo "Back to main page"; } else { echo "ERROR"; } ?> <?php // close connection mysql_close(); ?> [/php]

if there uploading but you cannot open them it maybe a permissions issue check you have the right permissions set on the folder you uploading into.

One of my $target variables had the wrong name. That’s why I couldn’t upload both files at once.

What’s the best way to go about getting checks in place for file types?

I tend to compare the type part of the file array like this:

[php]
// location where inital upload will be moved to
$target = “assets/plugins/gallery/” . $_FILES[‘uploaded’][‘name’] ;

// find thevtype of image
switch ($_FILES[“uploaded”][“type”]) {
case $_FILES[“uploaded”][“type”] == “image/gif”:
move_uploaded_file($_FILES[“uploaded”][“tmp_name”],$target);
break;
case $_FILES[“uploaded”][“type”] == “image/jpeg”:
move_uploaded_file($_FILES[“uploaded”][“tmp_name”],$target);
break;
case $_FILES[“uploaded”][“type”] == “image/pjpeg”:
move_uploaded_file($_FILES[“uploaded”][“tmp_name”],$target);
break;
case $_FILES[“uploaded”][“type”] == “image/png”:
move_uploaded_file($_FILES[“uploaded”][“tmp_name”],$target);
break;
case $_FILES[“uploaded”][“type”] == “image/x-png”:
move_uploaded_file($_FILES[“uploaded”][“tmp_name”],$target);
break;

default:
$error[] = ‘Wrong image type selected. Only JPG, PNG or GIF accepted!.’;
}
[/php]

you can easily extend the files it approves I guess you could also have an array of valid extensions instad of lots of if statements.

Sponsor our Newsletter | Privacy Policy | Terms of Service