Jump to content

Image uplaod not working


bravo14

Recommended Posts

I have a form that has some predefined images that are already uploaded, if however the user wants to add a new picture they select other from the form and then the image should be uploaded and added to database.  The upload and add code is below.

 

It appears that if I select one of the predefined images then the value is added to the db without any problems, the problem arises when a new image is to be uplaoded.

 

The code to add and uplaoad

 

 

<?php
  if(isset($_POST['submit'])){
      //set variables from form
      $identifier=$_POST['identifier'];
      $headline=$_POST['headline'];
      $content=addslashes($_POST['content']);
   //echo $content;
      $photo=$_POST['image'];
   echo $photo;
      if($photo!='other'){
          $image=$photo;
      }
      else{
          //upload image
          $filename = $_FILES["file"]["name"];
 $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
 $file_ext = substr($filename, strripos($filename, '.')); // get file name
 $filesize = $_FILES["file"]["size"];
 $allowed_file_types = array('.jpg','.png','.gif','.bmp');
    //echo $file_basename;
 //echo $file_ext;
 if (in_array($file_ext,$allowed_file_types)  &&  ($filesize < 200000)) {
  // rename file
  $newfilename = mktime() . $file_ext;
  echo $newfilename;
  if (file_exists("http://www.jasondoyleracing.com/news-images/" . $newfilename)) {
   // file already exists error
   $message= "You have already uploaded this file.";
  } else {
   move_uploaded_file($_FILES["file"]["tmp_name"], "http://www.jasondoyleracing.com/news-images/" . $newfilename);
          $image=$newfilename;
   }
}
}
      $today=date("Y-m-d");
   echo $today;
   preg_match("/<p[^>]*>(.*)<\/p>/",$content,$matches);
      //print_r($matches);
      $intro = strip_tags($matches[1]); //removes anchors and other tags from the intro
      //update page content
      echo $image;
   echo $intro;
      $add_news="INSERT INTO `tbl_news`(`identifier`,`headline`,`news_date`,`story`,`image`,`intro`)
VALUES ('$identifier','$headline','$today','$content','$image','$intro')";
      //echo $add_news;
      $add_news_sql=mysql_query($add_news)or die(mysql_error());
      $message="Your news story has been added";
     
}
?>
 

 

Any idea why the upload isn't working

Link to comment
Share on other sites

You can't use a URL in the move_uploaded_file() function for the destination parameter. Use a proper path and make sure the directory has write permissions.

// bad
move_uploaded_file($_FILES["file"]["tmp_name"], "http://www.jasondoyleracing.com/news-images/" . $newfilename);

// good
move_uploaded_file($_FILES["file"]["tmp_name"], "/public_html/news-images/" . $newfilename);
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.