Jump to content

Two file uploads within a form - going to different paths


applebiz89

Recommended Posts

I have a form that lets a user upload an image, aswell as another file. I want the image to go into the images directory, and the pdf file to go into the PDF directory once uploaded. The names of the files will then be uploaded to the database.

 

At the moment I can get the image to upload to the database and directory, but the PDF is only uploading the name to the database, it is not going into the PDF directory aswell. I'm not sure if there is a way you can do this, I am trying to use "move_uploaded_file()" to physically move the files but not sure if you can use it twice.

 

here is my code, if you can point anything out or where im going wrong that would be great.

<?php

include "include/conn.php";

include "include/session.php";
?>

<?php


// Check to see if the type of file uploaded is a valid image type
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 for image
$TARGET_PATH = dirname(__FILE__) . "/images/";

//Target path for PDF files
$PDF_TARGET_PATH = dirname(_FILE_) . "/PDF/";

// Get POST variables

$image = $_FILES['image'];

$pdf = $_FILES['pdf'];

$date= date("Y-m-d ");
$time = date("H:i:s");

$title = $_POST['title'];
$title_escape = mysql_escape_string($title);
$title_slashes = stripslashes($title_escape);

$content = $_POST['content'];

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

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


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

//PDF path
$PDF_TARGET_PATH .= $pdf['file_name'];

// Make sure all the fields from the form have inputs
if ($image['name'] == "" )
{
        $_SESSION['error'] = "All fields are required";
        header("Location: add_media.php");
        exit;
}

// Verify file is an image
if (!is_valid_type($image))
{
        $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
        header("Location: add_media.php");
        exit;
}



// move file into the directory and the path name into the database / along with the rest of the form fields
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
        if(move_uploaded_file($pdf['tmp_name'], $PDF_TARGET_PATH))
	{
	$sql = "INSERT INTO media_table(title, text, time, date, image, PDF) VALUES ('" . $title_slashes . "','" . $content . "', '" . $time . "', '" . $date . "', '" . $image['name'] . "', '" . $pdf['name'] . "')";
        $result = mysql_query($sql) or die (mysql_error());
        header("Location: add_media.php");
        exit;
	}
	else
	{
		print "did not upload";
	}
}
else
{
        
        $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
     
        exit;
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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