Jump to content

Recommended Posts

Hey fellow PHPers,

 

I am currently trying to upload an image to my mysql file server.  It seems to go through however the file is never actually saved on the server.  Attached is my code and hopefully someone can catch my mistake.

 

            <form name="form1" method = "post" enctype = "multipart/form-data">

 

      Photographer name: <input type="text" name="authorname" size="40" />      <br />

      Upload File: <input type="file" name="filename" id = "filename" size="40" />    <br />

      File Type: <select name = "select">

                  <option value = "JPEG">JPEG</option>

                  <option value = "GIF">GIF</option>

                  <option value = "PNG">PNG</option>

                  <option value = "other">Other</option>

                  </select>

            <br />

      Photo Name: <input type = "text" name = "photoname" />          <br />

      Photo Description: <input type = "text" name = "photodescription" />

                                                                <br />

 

      <input type="submit" value = "submit" />

        <input type="reset" value = "reset"/>

 

 

    </form>

   

<?php

include("connect.php") ;

$path = "/images/" ;

$authname = $_POST['authorname'];

$phototype = $_POST['select'];

$photoitself = $_POST['filename'];

$photoname = $_POST['photoname'];

$photodesc = $_POST['photodescription'];

   

 

 

move_uploaded_file($files['filename'] ['tmp_name'],"/public_html/images/" . $_FILES['filename']['name']);

echo "stored in: " . "images/" . $_FILES['filename']['name'];

 

 

?>

 

the addphoto3.php is stored under the public_html folder of my web server, and there is an images folder within the public_html where i want the images to be stored.  Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/43082-uploading-an-image-to-a-file-server/
Share on other sites

You should change this line:

move_uploaded_file($files['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name']);

to:

move_uploaded_file($_FILES['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name']);

 

If that doesn't help, you could change the above to:

 

if (move_uploaded_file($_FILES['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name'])) {
  echo 'Image uploaded!';
} else {
  echo 'Error: '.$_FILES['filename']['error'];
}

 

And then look up the error code here: http://us3.php.net/manual/en/features.file-upload.errors.php

 

Also, is addphoto3.php the PHP code you posted? If so, then you should remove '/public_html/' from the above line.

hey the bat,

 

these are the error messages i receive:

 

Warning: move_uploaded_file(/public_html/images/doggy.jpg): failed to open stream: No such file or directory in /home/welsh/public_html/addphoto3.php on line 55

 

Warning: move_uploaded_file(): Unable to move '/tmp/phpn64U28' to '/public_html/images/doggy.jpg' in /home/welsh/public_html/addphoto3.php on line 55

Error: 0

 

for the code:

 

include("connect.php") ;

$path = "/images/" ;

$authname = $_POST['authorname'];

$phototype = $_POST['select'];

$photoitself = $_POST['filename'];

$photoname = $_POST['photoname'];

$photodesc = $_POST['photodescription'];

 

 

 

if (move_uploaded_file($_FILES['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name'])) {

  echo 'Image uploaded!';

} else {

  echo 'Error: '.$_FILES['filename']['error'];

}

 

 

does this have something to do with the fopen/fread commands? and if it does what would you suggest using.  Since the code above is addphoto3, it is in the public_html folder hence i removed that part from where it will be saved to just images/.

hey the bat, the last post was made in error (i had forgot to delete the public_html from the code). It says image uploaded however it uploads the php file (addphoto3 to the images folder) and not the image itself.  Any idea on how to get the image itself in the folder?

Hmm,

 

I copied your code and modified it to work on my webserver. First, I gave the submit button a name, and then added an if statement around move_uploaded_file so that it only tries to upload when the submit button is clicked. Also, did you chmod your images folder to 777?

 

Here is the new code:

<form action="upload.php" method = "post" enctype = "multipart/form-data">

       Photographer name: <input type="text" name="authorname" size="40" />      

       Upload File: <input type="file" name="filename" id = "filename" size="40" />     

       File Type: <select name = "select">
                  <option value = "JPEG">JPEG</option>
                  <option value = "GIF">GIF</option>
                  <option value = "PNG">PNG</option>
                  <option value = "other">Other</option>
                  </select>
             

       Photo Name: <input type = "text" name = "photoname" />           

       Photo Description: <input type = "text" name = "photodescription" />
                                                                 


       <input name="submit" type="submit" value = "submit"/>
        <input type="reset" value = "reset"/>


    </form>
    
<?php
include("connect.php");
$path = "/images/";
$authname = $_POST['authorname'];
$phototype = $_POST['select'];
$photoitself = $_POST['filename'];
$photoname = $_POST['photoname'];
$photodesc = $_POST['photodescription'];

if ($_POST['submit']) {
   if (move_uploaded_file($_FILES['filename']['tmp_name'], $path.$_FILES['filename']['name'])) {
      echo 'Image uploaded!';
   } else {
      echo 'Sorry, your image wasn\'t uploaded. Upload error: '.$_FILES['filename']['error'];
   }
}
?>

 

That should do it for you. Good luck!

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.