PHP图像上传和唯一文件名的分配

时间:2014-03-08 03:36:02

标签: php sql image upload uniqueidentifier

提前谢谢你。我检查了类似的问题,他们没有帮助,因为工作流程设置不同。 试着去工作: 1.user通过表单字段上传图像     
2.(脚本1)在其他页面脚本上分配唯一名称(SCRIPT 2),将图像文件保存到服务器,图像URL上传到SQL。在脚本结束时转到新页面。

问题是我没有收到错误,脚本运行并且新页面打开但是服务器上没有保存文件,并且没有数据插入SQL表(条目日期添加但不添加图像URL)。我的PHP.ini指令远远超过了我一直在测试的图像的大小。文件夹位置是chamode 0777.我发布了整个脚本,因为收到错误很难看出问题所在。

图像处理

  <?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);     
  ?> 

独特的发电机

  <?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;
      }
  ?>

1 个答案:

答案 0 :(得分:0)

尝试将其放在脚本的顶部:

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

这在底部:

<?php
    print_r(array_keys(get_defined_vars()));
    print_r(array_values(get_defined_vars()));
?>