Reading image files causing error - Uncaught ReferenceError

896 views php
8

I am reading two image files and storing into psql. When I read one file, the code doesn't raise error. When I add code for other file, the code starts complaining 'Uncaught ReferenceError' about one script at the end of the html.

Here is my code. Is this correct way to read two image files or there is some problem the way I am trying to read the files.

if(isset($_POST["submit1"])){
    $file_name = $_FILES["img1"]["tmp_name"];
    $img = fopen($file_name, 'r') or die("cannot read image\n"); 

    $data = fread($img, filesize($file_name));
    var_dump($file_name);

    $es_data = pg_escape_bytea($data);
    fclose($img);  

    $file_name2 = $_FILES["img2"]["tmp_name2"];
    $img2 = fopen($file_name2, 'r') or die("cannot read image\n"); 

    $data2 = fread($img2, filesize($file_name2));
    var_dump($file_name2);

    $es_data2 = pg_escape_bytea($data2);
    fclose($img2);

    try {

        $sql .......................

answered question

$file_name2 = $_FILES["img2"]["tmp_name"];

1 Answer

1

$_FILES is an multidimensional array. The first column/element represents the given file, the second an attribute corresponding to that file. So tmp_name will always stay the same while img will probably change. But you could also enter a 3rd dimension to handle multiple files.

<form action="file-upload.php" method="post" enctype="multipart/form-data">
 Send these files:<br />
   <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

$_FILES["userfile"]["tmp_name"][0] would be the name of the first file, $_FILES["userfile"]["tmp_name"][1] the name of the second one, ...

https://secure.php.net/manual/en/features.file-upload.multiple.php

posted this

Have an answer?

JD

Please login first before posting an answer.