Domanda

Thank you in advance. I've checked similar questions and they are not helping because the work flow is set up differently. Trying to get working: 1.user uploads image via form field
2.(SCRIPT 1) on other page script assigns unique name (SCRIPT 2), saves image file to server and image URL is uploaded to SQL. Goes to new page at end of script.

Problem is I'm not getting errors, the script runs and the new page opens but there is no file saved on the server and no data inserted into the SQL table (the entry date adds but not the image URL). My PHP.ini instructions far exceeds the size of the images I've been testing with. The folder location is chamode 0777. I'm posting the whole script because with getting errors it's hard to see where problem lies.

Image processing

  <?php
  require_once 'unique_gen.php';
      $page_path = $_POST['page_path'];
      $imgloc = "/avatars/";
  //up one directory level
      $store_loc = "..".$imgloc;
      $link_loc = "http://www.webapge.com".$imgloc;
  //Upload and characterize image file 
    if(isset($_FILES['image'])){
  //File 
      $upload['image'] = $_FILES['image'];
  //Verify 
          if ($upload['image']["error"] > 0){
            die ("File Upload Error: " . $upload['image']["error"]);
          }else{
  //Upload 
            $img_ext = end(explode('.', $upload['image']['name']));
  //Unique code generator
            $image_name = implode('.', array(unique_generator(),$img_ext));
              while(file_exists($store_loc.$image_name)){
                $image_name = implode('.', array(unique_generator(),$img_ext));
               }
          $image_name = $upload['image']['name'];
          //Move file to another location
          move_uploaded_file($upload['image']["tmp_name"],$store_loc.$image_name) or exit("<br>Error, IMAGE file not moved!");
          //Save location as link
          $link_to_img = $link_loc.$image_name;
          }
    }else{
      $image_name = "";
    }

  //connect to db
  $con=mysqli_connect("localhost","usernm","pssword","dbName");

  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

  //Insert to SQL
  $sql="INSERT INTO comments (avatar, entry_date)
  VALUES
  ('$_POST[link_to_image]', now())";

  //verify insert
  if (!mysqli_query($con,$sql))
    {
    die('Error: ' . mysqli_error($con));
    }
  //direct to new page using variable
  header('Location: http://www.weBsite.com/' . $page_path);
  //close session
  mysqli_close($con);     
  ?> 

Unique generator

  <?php
      function unique_generator($lot_size = 15){
          $alpha_s = range('a', 'z');
          $alpha_l = range('A', 'Z');
          $numbers = range(0, 9);
          $char = array_merge($alpha_l,$alpha_s,$numbers);
          $code = "";
          for($i = 0; $i < $lot_size; $i++){
              $key = rand(0,count($char)-1);
              $code .= $char[$key];
          }
          return $code;
      }
  ?>
È stato utile?

Soluzione

Try putting this at the top of your script:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','1');
?>

This at the bottom:

<?php
    print_r(array_keys(get_defined_vars()));
    print_r(array_values(get_defined_vars()));
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top