Jump to content

Three errors in upload script?


Mr Chris

Recommended Posts

Hi All,

I’m tying to put together an image uploading script from what I’ve seen around on t’internet which saves the details of the uploaded image to a MYSQL database.

Now, I’ve kind of got it working apart from three problems:

[code=php:0]
<form action="script.php" method="post" enctype="multipart/form-data" name="form1">
  <table width="247" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th width="247">
<input name="file[]" type="file" id="file[]"></th></tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td></tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td></tr>
    <tr>
      <td><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td></tr>
  </table>
</form>
[/code]

[code=php:0]
<?

// Start the connection to the database
         
        include('*****’);
        $can_i_connect = db_connect(); // by db_connect function is in my include file
        if(!$can_i_connect)
        {
            echo "Could not connect to database";
        }

// End the connection to the database   

//get array of filenames
foreach($_FILES['file']['name'] as $value2) {
$filename .= "Image Name: $value2\n";
}
echo $filename;
echo $value2;

//validate file 1
function filecheck($value2){
   $ext = strrchr($value2,'.');
   $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");
   if ( in_array ($ext, $FileType) )
     return TRUE;
   else
     return FALSE;
}



foreach ($_FILES['file']['name'] as $k => $filename) {

if ($filename != '')  {

  $tmpfile = $_FILES['file']['tmp_name'][$k];

  }else{
  echo "error";
  }
   
//if the temp file1 is there copy it to the server
if (@is_uploaded_file($tmpfile)) {
copy($tmpfile, "return_images/" .$filename);
echo "<br>success";
}else {
echo "<br>Not Uploaded<br>";
}

}

foreach ($_FILES['file']['name'] as $value3) {


$pathname .= "$value3";
$mysql = "**********";
mysql_select_db($mysql);
$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";

mysql_query($sql) or die('Error, insert query failed');

}
echo $pathname;
echo $thumbpath;
?>
[/code]

1) When I upload 3 images the pathnames for the images duplicate themselves into the MySQL database like so:

[img]http://www.slougheaz.org/property/one.jpg[/img]


2) When I upload just one image (not three) the script throws up errors for the missing two uploads not chosen:
[img]http://www.slougheaz.org/property/two.jpg[/img]

3) Then also when you do (2) the data in the Mysql database duplicates itself even though you only uploaded one image?

[img]http://www.slougheaz.org/property/three.jpg[/img]


Can anyone offer some advise to why this is doing these three things?

Thanks

Chris
Link to comment
https://forums.phpfreaks.com/topic/30225-three-errors-in-upload-script/
Share on other sites

1) & 2) & 3) Modify some code
[code]
<?php
...
...
...
foreach ($_FILES['file']['name'] as $k => $filename) {

if ($filename != '')  {

  $tmpfile = $_FILES['file']['tmp_name'][$k];

//mansuang: move this code here
//if the temp file1 is there copy it to the server
if (@is_uploaded_file($tmpfile)) {
copy($tmpfile, "return_images/" .$filename);
echo "<br>success";
}else {
echo "<br>Not Uploaded<br>";
}
//mansuang: end of moving the code


  }else{
  echo "error";
  }
 

}

foreach ($_FILES['file']['name'] as $value3) {

if(!empty($value3)){ //mansuang:Add "if" statement
$pathname = "$value3"; //mansuang:original one-> $pathname .= "$value3";
$mysql = "**********";
mysql_select_db($mysql);
$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";

mysql_query($sql) or die('Error, insert query failed');
} //mansuang:close "if" statement
}
echo $pathname;
echo $thumbpath;
?>
[/code]

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.