I've got a problem uploading a file. I'm uploading it as binary data, and I am therefore using fopen to get the file contents. The script I'm using is as follows:

if ($fileUpload)
{
$picture = addslashes(fread(fopen($fileUpload, "rb"), filesize($fileUpload)));

$picture_type=$fileUpload_type;
echo $picture;

$update_picture_query="UPDATE user SET picture='$picture', picture_type='$picture_type' WHERE username='$username'";
mysql_query($update_picture_query);
}

The script does work when run as part of a script on another page, but when I run it separately, I get the following error messages:

Warning: fopen(U:\Nina\Experimentversioner\Parthenon\medarbetare\competenskraft\bilder\Helena Jönsson.jpg) [function.fopen]: failed to create stream: No such file or directory in E:\www\Parthenon\content_types\user_details.php on line 26

Warning: filesize() [function.filesize]: Stat failed for U:\Nina\Experimentversioner\Parthenon\medarbetare\competenskraft\bilder\Helena Jönsson.jpg (errno=2 - No such file or directory) in E:\www\Parthenon\content_types\user_details.php on line 26

Warning: fread(): supplied argument is not a valid stream resource in E:\www\Parthenon\content_types\user_details.php on line 26

Does anybody have any clues as to where the problem lies?

/Nina

    Hi

    I had the same problem when I was trying to access a file with non-english charaters like ö and ø under windows 2000.

    Remove these charaters and you should be able to access the file.

    BTW are you sure that the path is valid?

      I have already tried it with a few other pictures, all with valid english filenames, but I still get the same error. Besides, uploading that same picture worked in my other script.

      The path should be fine; I'm using a form to browse for the image. I've also double-checked the path, just to make sure.

      It's all rather strange, isn't it?

        I would make the read in two steps:

        $filehandle = fopen($fileupload,"rb"); // first open a handle to the file
        if ($filehandle) {
        print"The file exists!";
        $picture = addslashes(fread ($filehandle, filesize ($fileupload)));
        fclose ($filehandle);
        }
        else {
        print"The file does not exist";
        }

        Make sure you have read access to file.

          Originally posted by newcastle_unite
          The path should be fine; I'm using a form to browse for the image. I've also double-checked the path, just to make sure.

          Can I just check something: are you saying that $fileupload contains information from a form field? Or have you already uploaded the file and are now trying to copy it from its temporary upload directory into the database?

            $fileUpload contains information from a form field - the path to the file to be uploaded.

              I've now tried doing it in the two suggested steps as well, with the same result as previously...

                You cannot access a file from a client pc.

                You have to upload the file to the web server first before you can use fopen/fread etc....

                  But I've done the exact same thing in several other scripts, and its worked flawlessly??

                    The script will work if
                    you are testing your script on your server meaning client = server pc.

                    or

                    the server has access to a directory and a file with the same name as the one you are trying to upload

                      I'm sorry to be so fussy, but I don't understand this at all.

                      I'm not running the script from the server. Uploading the exact same file from the exact same location has worked in other scripts, located on the same server as the faulty script.

                      How can it work in other scripts, if you're not supposed to be able to access files from a client PC?

                        I need to see the other scripts before I can tell why these scripts work but this one does not

                          This is the originating script. I've used it in several other places, and it works flawlessly everywhere else... In the not-working script I'm getting the script as an include, but I have also tried pasting it into the originating page, but with the same result.

                          if ($fileUpload)
                          {

                          $picture = addslashes(fread(fopen($fileUpload, "rb"), filesize($fileUpload)));

                          $insert="UPDATE user SET picture='$picture', picture_type='$fileUpload_type' WHERE username='$username'";

                          	mysql_query($insert);
                          
                          	echo $fileUpload_name;
                          	echo "<BR>";		
                          	echo $fileUpload_type;
                          	$picture_type=$fileUpload_type;
                          	echo "<BR>";
                          	echo $fileUpload_size;
                          	echo "<BR>";
                          	echo $beskrivning;
                          	echo "<BR>";
                          
                          }

                            I have no idea why one script does work and the other does not work, but the following code should work, even with multiple files.

                            <?php
                            
                            function upload($FVARS) {
                              for($i=0;$i<count($FVARS[file][tmp_name]);$i++) {
                                $fileUpload_size=$FVARS[file][size][$i];     // filesize
                                $fileUpload_type=$FVARS[file][type][$i];     // mime type 
                                $fileUpload_name=$FVARS[file][name][$i];     // original name
                                $temp=$FVARS[file][tmp_name][$i]; // temporary name
                                if($size) { 
                            
                            
                                //whatever to do with uploaded files
                                echo "original name: $fileUpload_name<br>";
                                echo "temporary name: $temp<br>";
                                echo "mime type: $fileUpload_type<br>";
                                echo "size: $fileUpload_size<hr>";
                                $picture = addslashes(fread(fopen($temp, "rb"), filesize($temp))); 
                            
                            $insert="UPDATE user SET picture='$picture', picture_type='$fileUpload_type' WHERE username='$username'"; 
                            
                            mysql_query($insert); 
                            }
                              }
                            }
                            ?>
                            
                            <html><body>
                            
                            <?php
                            if ($_GET['act'] == "upload") {
                              upload($_FILES); 
                              exit();
                            }
                            print "<form ENCTYPE=\"multipart/form-data\" method=\"POST\" action=\"$PHP_SELF?act=upload\">";
                            ?>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            File:<INPUT TYPE="file" NAME="file[]" SIZE="70"><BR>
                            <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
                            
                            <input type="submit" value="Upload">
                            <INPUT TYPE="RESET" />
                            </form>
                            Please click only <b>once</b> and wait for confirmation
                            </body></html>
                            
                              Write a Reply...