-1

I found some code that uploads a file to a folder on my server and then saves the path to a database table column. the code for the form:

 <form method="post" action="add_member.php" enctype="multipart/form-
 data">
<p>Please Upload a Photo of the Member in gif or jpeg format. The file  
name should be named after the Members name. If the same file name is  
uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
        <p>
          Photo:
        </p>
        <input type="hidden" name="size" value="350000">
        <input type="file" name="cert_1">
           <br/>
            <br/>
          <input TYPE="submit" name="upload" title="Add data to the 
Database" value="Add Member"/>
</form>

And then the script with the algo that moves the file to the upload folder and then adds the full path to the database column

//This is the directory where images will be saved
 $target = "upload";
 $target = $target . basename( $_FILES['cert_1']['name']);

 //This gets all the other information from the form

   $pic=($_FILES['cert_1']['name']);



// Connects to your Database
mysql_connect("host", "db_user", "_db_pass") or         

die(mysql_error()) ;
mysql_select_db("your_db") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO student_biodata_master 
           (cert_1)
           VALUES ('$pic')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['cert_1']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['cert_1']['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 file.";
}

Now, I need to increase the number of file upload fields to four at the least and then write the full path of the files to the database columns respectively. The script works very well but only does not write the full path into the database so i can call it later and display in my application. Can "anygoodbody" help with this? its actually doing my head in.

Thanks in advance for the great advice always.

Stack Overflow rocks!

Ralph E. Emeka
  • 15
  • 1
  • 2
  • 8
  • 1
    You are vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Aug 09 '16 at 18:46
  • This is answered here http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php – Emma Aug 09 '16 at 18:48
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Aug 09 '16 at 21:00
  • Thanks @tadman i will surely do that. Thanks too Marc B. I have had a look Emma. Thanks. I eventually found a secure was with a new jquery plug. I also retorted to using PDO instead. – Ralph E. Emeka Sep 28 '18 at 11:18

1 Answers1

0

You need html5 supported browser for multiple upload or use different technologies: java applet, flash, silverlight. jquery has a plugin for multiple upload: http://blueimp.github.io/jQuery-File-Upload/

mkysoft
  • 5,392
  • 1
  • 21
  • 30
  • *"You need html5 supported browser for multiple upload"* - Not really; PHP can handle multiples. – Funk Forty Niner Aug 09 '16 at 18:48
  • Java applet? Silverlight? Flash? Depending on any of those is a mistake, they're all dead or dying. HTML5 is supported by pretty much everything, there's no worry about using jQuery in conjunction with that. – tadman Aug 09 '16 at 21:02
  • @Fred-ii- only html5 supported browser send multiple files or java applet/flash/silverlight. I didn't talk about server side. tadman did you check html5 browser usage on the world? I try to give attention to user for choosing right technologies for problem. – mkysoft Aug 10 '16 at 12:22
  • This works. Thanks mkysoft, I don't know if i want to go all the way with applets with the little support and the cost of using flash today. I think jQuery gives me all of that in one. – Ralph E. Emeka Sep 28 '18 at 11:19