Jump to content

PHP code can't write more than one file to MySQL database


josephbupe

Recommended Posts

Hi,

 

I have a code am trying to modify in order to allow writing more than one file to a MySQL database. Initially, the code was able to write one file into the database along with other information and saving the actual file into a directory. But after adding extra lines of code this isn't possible any more. I am not getting any error message.

 

Kindly assist.

 

Please, see attached file upload.php file.

upload.php

Link to comment
Share on other sites

Thank you muddy_funster for your early response.

 

Here is my upload.php code highlighting additions i made to uploading a second image (image2) and third image (image3) for the same record:

 

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
 // This is an array that holds all the valid image MIME types
 $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");
$valid_types = array("image2/jpg", "image2/jpeg", "image2/bmp", "image2/gif");
$valid_types = array("image3/jpg", "image3/jpeg", "image3/bmp", "image3/gif");

 if (in_array($file['type'], $valid_types))
	 return 1;
 return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
 echo "<pre>";
 print_r($array);
 echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$ctitle = $_POST['ctitle'];
$csubject = $_POST['csubject'];
$creference = $_POST['creference'];
$cyear = $_POST['cyear'];
$cwidth = $_POST['cwidth'];
$cheight = $_POST['cheight'];
$cperiod = $_POST['cperiod'];
$cdescription = $_POST['cdescription'];
$image = $_FILES['image'];
$image2 = $_FILES['image2'];
$image3 = $_FILES['image3'];

// Sanitize our inputs
$ctitle = mysql_real_escape_string($ctitle);
$csubject= mysql_real_escape_string($csubject);
$creference = mysql_real_escape_string($creference);
$cyear = mysql_real_escape_string($cyear);
$cwidth = mysql_real_escape_string($cwidth);
$cheight = mysql_real_escape_string($cheight);
$cperiod = mysql_real_escape_string($cperiod);
$cdescription = mysql_real_escape_string($cdescription);
$image['name'] = mysql_real_escape_string($image['name']);
$image2['name2'] = mysql_real_escape_string($image2['name2']);
$image3['name3'] = mysql_real_escape_string($image3['name3']);

// Build our target path full string. This is where the file will be moved do
// i.e. images/picture.jpg
$TARGET_PATH .= $image['name'];
$TARGET_PATH .= $image2['name2'];
$TARGET_PATH .= $image3['name3'];

// Make sure all the fields from the form have inputs
if ( $ctitle == "" || $csubject == "" || $creference == "" || $cyear == "" || $cwidth == "" || $cheight == "" || $cperiod == "" || $cdescription == "" || $image['name'] == "" || $image2['name2'] == "" || $image3['name3'] == "" )
{
 $_SESSION['error'] = "All fields are required";
 header("Location: index.php");
 exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
 $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
 header("Location: insert.php");
 exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
 $_SESSION['error'] = "A file with that name already exists";
 header("Location: insert.php");
 exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
 // NOTE: This is where a lot of people make mistakes.
 // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
 $sql = "insert into collections (ctitle, csubject, creference, cyear, cwidth, cheight, cperiod, cdescription, cfilename, cfilename2, cfilename3) values ('$ctitle', '$csubject', '$creference', '$cyear', '$cwidth', '$cheight', '$cperiod', '$cdescription', '" . $image['name'] . "', '" . $image2['name2'] . "', '" . $image3['name3'] . "')";
 $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
 header("Location: index.php");
 exit;
}
else
{
 // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
 // Make sure you chmod the directory to be writeable
 $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory";
 header("Location: index.php");
 exit;
}
?>

 

And also part of my insert.php file:

 

<tr>
<td>Upload Image1</td><td></td>
 <td> <input type="file" name="image" /><br />
				 <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</tr>
<tr>
</td>

<td>Upload Image2</td><td></td>
 <td> <input type="file" name="[color=#0000ff]image2[/color]" /><br />
				 <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</tr>
<tr>
</td>

<td>Upload Image3</td><td></td>
 <td> <input type="file" name="[color=#0000ff]image3[/color]" /><br />
				 <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
				 <input type="submit" id="submit" value="Save" />
</td>
</tr>

 

I will appreciate your help.

 

joseph

Edited by josephbupe
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.