i can upload to MYSQL, with user generated content, picture included...

when i access the information, everything appears but the picture, just the pictures file name is listed (no pic)

is my code wrong?... am i missing something?
would appreciate any help

define(`uploadpath`, `htdocs/phpsessions/images`);


{  

 
$pix=$_FILES['pix']['name'];
$target=uploadpath . $pix;
echo"$target";
if(move_uploaded_file($pix, $target)){
// do your coding here to give a thanks message or any other thing. 
}else{echo "Failed to upload file Contact Site admin to fix the problem";}






    $user="user";
    $host="website";
    $passwd="password";
    $dbname="alpha";
     $cxn = mysql_connect('website', 'user', 'passord'); if (!$cxn) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_select_db(alpha); 
    $sql = "INSERT INTO details (`classificationss`,`condition`,`type`,`price`,`region`,`size`,`comment`,`pix`) VALUES ('$classificationss', '$condition','$type','$price','$region','$size','$comment','$pix')";
    $result = mysql_query($sql,$cxn)
               or die("Couldn't execute ? query: " . mysql_error()); 

                   $_SESSION['auth']="yes";                          
                   $_SESSION['logname'] = $loginName;    
                     
      
        header("Location: New_member.php");               

     
          } 
  break;                                                

default:                                              
    include("login_form2.inc");


  /*end of thing3*/


}  
?>
<? ob_flush(); ?>

Dani AI

Generated

Short diagnosis and what the thread already shows: was right to point out that the temporary name (the tmp_name entry) must be used, and correctly pushed filesystem paths and permissions. The symptoms in ’s later posts — an empty $_FILES printout and the "chmod: No such file or directory" warning — strongly point to one or more of these problems: the form not using enctype="multipart/form-data", an input-name mismatch (e.g. using pix in PHP but image in HTML), PHP limits or open_basedir restrictions that prevent uploads, or a wrong/URL-style destination path so move_uploaded_file() never actually writes the file.

A practical troubleshooting checklist:

  • Confirm the HTML form uses method="post" and enctype="multipart/form-data" and the file input name matches the $_FILES key.
  • Immediately inspect $_FILES (use print_r($_FILES)), and check $_FILES['pix']['error'] for UPLOADERR* codes. An empty $_FILES usually means the form encoding or size limits failed.
  • Use a filesystem path for the destination (not a URL). Verify the directory exists and is writable by the webserver (is_dir(), is_writable()).
  • After move_uploaded_file() verify file_exists() before calling chmod(); the chmod warning means the destination file wasn’t created.
  • Check php.ini values: upload_max_filesize, post_max_size, upload_tmp_dir, and any open_basedir restrictions.

Example pattern (adjust paths and names to the site; this is intentionally different from earlier snippets):

$uploadDir = __DIR__ . '/images/';            // filesystem directory
if (!empty($_FILES['pix'])) {
    $f = $_FILES['pix'];
    if ($f['error'] === UPLOAD_ERR_OK && is_uploaded_file($f['tmp_name'])) {
        $name = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($f['name']));
        if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
        $dest = $uploadDir . $name;
        if (move_uploaded_file($f['tmp_name'], $dest)) {
            chmod($dest, 0644);
            // store $name in DB and reference it with '/images/'.$name for display
        } else {
            error_log('move_uploaded_file failed: ' . print_r(error_get_last(), true));
        }
    } else {
        error_log('Upload error code: ' . $f['error']);
    }
}

Key takeaway: the most frequent causes in this thread are a wrong form encoding or input-name mismatch (explains the empty $_FILES), and using a web path instead of a filesystem path when moving the file (explains the chmod/file-not-found message). The code above follows the checks needed to surface which case is happening and to ensure the file is actually created before changing permissions or inserting the filename into the database.

Recommended Answers

All 14 Replies

Member Avatar for Member #120589
$pix=$_FILES['pix']['name'];
$target=uploadpath . $pix;
echo"$target";
if(move_uploaded_file($pix, $target)){

shouldn't that be:

$pix=$_FILES['pix']['name'];
$pixtmp = $_FILES['pix']['tmp_name'];
$target=uploadpath . $pix;
echo"$target";
if(move_uploaded_file($pixtmp, $target)){

thank you, i tried your script change, but unfortunately my pix still do not "move" to the "images" folder where my html docs are located..when i call up the data everything appears, but the pix, which is represented by its filename. as indicated below...

pneus new winter 0-50 Abitibi-Tamiscamingue P1558013 work photo.JPG ..


can it be related to this?

define('uploadpath', 'root/htdocs/phpsessions/images');

i am completely lost..:(

Member Avatar for Member #120589

try making the upload path relative to the script itself.
e.g. if the script is in "/includes/", and if your upload directory is "/images/", you could do this:

define('uploadpath', '../images/');

If you really have to use root:

define('uploadpath', $_SERVER['DOCUMENT_ROOT'] . "/images/");

then that shoulkd work fine when you:

$target=uploadpath . $pix;

first off i would like to thank you again for your help...
unfortunately im getting the results however i did add this script to see if i successfully transfer and

define('uploadpath', $_SERVER . "/images/");

$pix=$_FILES;
$pixtmp = $_FILES;
$target=uploadpath . $pix;
echo "$target";
if(move_uploaded_file($pixtmp, $target)){

echo "File is valid, and was successfully moved.\n";
} else {
echo "not moved";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

which loads with page, don't know if this code is helping or hindering, however, when i "SELECT" from database, it still only gives me the filename, and the image is not in my "images" folder..

home/users/web/b2947/pow.xyz/htdocs/images/not uploaded Here is some more debugging info:Array ( )

if you need more code i would be more than happy
thanks ron

Are you saving the upload path to the database and trying to use that path to display the image?

Are you saving the upload path to the database and trying to use that path to display the image?

thanks for responding, i hope this is what you were asking..

the image gets successfully uploaded into mysql...everything i have read says i have to move that image from its temporary folder within the server root to where my html documents reside, where i created a folder called "images"

im not anywhere near the novice level of code writing(re:super beginner), i understand the point my book makes..however, for me, if the code was actually moved the picture file from the temporary folder to the "images" folder successfully, the picture should actually be in that folder?...

I don't know what you mean by "the image gets successfully uploaded into mysql" as images aren't saved into the database.

Anyway this is the code I use to move uploaded files from the temporary folder into your designated folder:

$name = basename($_FILES['image']['name']);
$NEW_IMAGE_PATH = "/images/" . $name;

move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);

I havent tested this actual script but something along these lines should do the trick.

I don't know what you mean by "the image gets successfully uploaded into mysql" as images aren't saved into the database.

Anyway this is the code I use to move uploaded files from the temporary folder into your designated folder:

$name = basename($_FILES['image']['name']);
$NEW_IMAGE_PATH = "/images/" . $name;

move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);

I havent tested this actual script but something along these lines should do the trick.

hi,
what i mean is that when i "browse" my database within the mysql environment, the file name, in this case, photo.jpg, is listed...my understanding is that the actual picture is in a temporary folder on the server..the book i have, says i then have to transfer that photo from that temp. folder to a folder where my htdocs are located..sounds simple enough..but..there's always but..when i try and "select" through another php doc, i get all the information posted with the picture and only picture name "photo.jpg".no picture, when i check my images folder, no image is present....

becasue i am sometimes dumber than a bag of hammers, can you verify if i made the proper adjustments to your code

$pix = basename($_FILES);
$NEW_IMAGE_PATH = "/images/" . $pix;
move_uploaded_file($_FILES, $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);

$name = basename($_FILES);
$NEW_IMAGE_PATH = "/images/" . $name;
move_uploaded_file($_FILES, $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);


thanks again

hi,
what i mean is that when i "browse" my database within the mysql environment, the file name, in this case, photo.jpg, is listed...my understanding is that the actual picture is in a temporary folder on the server..the book i have, says i then have to transfer that photo from that temp. folder to a folder where my htdocs are located..sounds simple enough..but..there's always but..when i try and "select" through another php doc, i get all the information posted with the picture and only picture name "photo.jpg".no picture, when i check my images folder, no image is present....

becasue i am sometimes dumber than a bag of hammers, can you verify if i made the proper adjustments to your code

$pix = basename($_FILES);
$NEW_IMAGE_PATH = "/images/" . $pix;
move_uploaded_file($_FILES, $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);

$name = basename($_FILES);
$NEW_IMAGE_PATH = "/images/" . $name;
move_uploaded_file($_FILES, $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);


thanks again

upon loading the page i got this

Warning: chmod() [function.chmod]: No such file or directory


help

When a file is uploaded it goes into a temporary folder only while the script is processing. As soon as the script has stopped working the file is deleted, so the file must be moved immediately after the upload before the end of the script.

This code should be placed after the upload script, but before it is inserted into the MYSQL database...

$name = basename($_FILES['pix']['name']); //GETS THE IMAGE NAME. 'PIX' SHOULD BE THE NAME OF THE UPLOAD INPUT BOX, E.G <input type="file" name="pix">
$NEW_IMAGE_PATH = "/images/" . $name; //SETS THE DIRECTORY YOU WANT THE IMAGE MOVING TO
move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH); //MOVES THE FILE
chmod($NEW_IMAGE_PATH, 0777); //MAKES SURE THE IMAGE HAS THE CORRECT PERMISSIONS

When a file is uploaded it goes into a temporary folder only while the script is processing. As soon as the script has stopped working the file is deleted, so the file must be moved immediately after the upload before the end of the script.

This code should be placed after the upload script, but before it is inserted into the MYSQL database...

$name = basename($_FILES['pix']['name']); //GETS THE IMAGE NAME. 'PIX' SHOULD BE THE NAME OF THE UPLOAD INPUT BOX, E.G <input type="file" name="pix">
$NEW_IMAGE_PATH = "/images/" . $name; //SETS THE DIRECTORY YOU WANT THE IMAGE MOVING TO
move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH); //MOVES THE FILE
chmod($NEW_IMAGE_PATH, 0777); //MAKES SURE THE IMAGE HAS THE CORRECT PERMISSIONS

thanks for the quick response..i copied my code maybe it will help, i think i have it in the right location???

<? ob_start(); ?>
<?php
/* Program: upload.php
 * Desc:    to sell
 *          (1) stuff 
 *          (2) stuff. 
 */

define('uploadpath', $_SERVER['DOCUMENT_ROOT'] . "/images/");





$pix = basename($_FILES['pix']['name']);
$NEW_IMAGE_PATH = "/images/" . $pix; 
move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH);
chmod($NEW_IMAGE_PATH, 0777);




session_start();                                          
switch (@$_POST['Button'])                                
{

/*thing1*/
  case "Register":                                      
   /* Check for blanks */
    foreach($_POST as $field => $value)                  
    {
      if($field != "comments")                                 
      {
        if(empty($value))
        {
          $blanks[] = $field;
        }
        else
        {
          $good_data[$field] = strip_tags(trim($value));
        }
      }
    }   
/*end of thing1*/
                                                 
                                                       


/*thing2*/
  /* validate data */
    foreach($_POST as $field => $value)                  
    {
      if(!empty($value))                                 
      {
        if(preg_match("/classificationss/i",$field)or
          preg_match("/condition/i",$field)or 
          preg_match("/type/i",$field)or
           preg_match("/price/i",$field) or 
           preg_match("/region/i",$field) or
           preg_match("/size/i",$field) or
           preg_match("/comment/i",$field) or
           preg_match("/pix/i",$field))
        {
          if(!preg_match("/^[A-Za-z0-9.,' -]{1,50}$/", 
                          $value))
          {
            $errors[] = "$value say what is not  valid.";
          }
        }


              } // end if not empty                             
    } 
    foreach($_POST as $field => $value)                 
    {
      $$field = strip_tags(trim($value));
    }
    if(@is_array($errors))                              
    { 
      $message_2 = "";
      foreach($errors as $value)
      {
        $message_2 .= $value." Please try again<br />";
      }
      include("login_form2.inc");
      exit();
    } 
// end if errors are found                        
/*end of thing2*/

 
                 

/*thing3*/
    {  
 
    $user="xxx";
    $host="rrr.powwebmysql.com";
    $passwd="yyy";
    $dbname="alpha";
        $cxn = mysql_connect('rrr.powwebmysql.com', 'xxx', 'yyy'); if (!$cxn) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_select_db(alpha); 
    $sql = "INSERT INTO details (`classificationss`,`condition`,`type`,`price`,`region`,`size`,`comment`,`pix`) VALUES ('$classificationss', '$condition','$type','$price','$region','$size','$comment','$pix')";
    $result = mysql_query($sql,$cxn)
               or die("Couldn't execute ? query: " . mysql_error()); 

    
               $_SESSION['auth']="yes";                          
      $_SESSION['logname'] = $loginName;    
                     
      /* send email to new Customer */

 
        header("Location: New_member.php");               


      $emess = "You have successfully registered. ";
      $emess .= "Your new user name and password are: ";
      $emess .= "\n\n\t$loginName\n\t";
      $emess .= "$password\n\n";
      $emess .= "We appreciate your interest. \n\n";
      $emess .= "If you have any questions or problems,";
      $emess .= " email outerbanx13@yahoo.com";          
      $subj = "Your new customer registration";         
     # $mailsend=mail("$email","$subj","$emess");       
     
          } 
  break;                                                

  default:                                              
    include("login_form2.inc");
/*end of thing3*/

}  
?>
<? ob_flush(); ?>

chmod($NEW_IMAGE_PATH, 0777);
my permissions is 644, would that make a difference

Thats seems like the right place to me, does the script work like that?

I usually use 777 but try 644 if you prefer it, see how it goes. PHP is all about trial and error. Very frustrating sometimes haha.

Thats seems like the right place to me, does the script work like that?

I usually use 777 but try 644 if you prefer it, see how it goes. PHP is all about trial and error. Very frustrating sometimes haha.

i will try tomorrow...new day maybe ill see the trees from the forest..ill keep you posted thanks

upon loading the page i got this

Warning: chmod() [function.chmod]: No such file or directory


help

define('NEW_IMAGE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/images/");



$pix = basename($_FILES['pix']['name']);
$NEW_IMAGE_PATH = "/images/" . $pix; 
move_uploaded_file($_FILES['image']['tmp_name'], $NEW_IMAGE_PATH);

chmod($NEW_IMAGE_PATH, 0644);


Warning: chmod() [function.chmod]: No such file or directory


im just wondering what would i do to define chmod...my web concierge says i have to define this..as he eloquently wrote

"First of all there is no temp file on your site. And, the error I see is a warning of that the chmod command is not recognized. Which could mean that the $NEW_IMAGE_PATH may not be defined properly in your php"


i think with your help i am close to resolving, kinda seeing the light at the end of the tunnel, just hope it is not another train...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.