Jump to content

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
https://forums.phpfreaks.com/topic/279786-image-uplaod-not-working/
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);
Edited by neil.johnson
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.