【问题标题】:PHP image upload and assignment of unique file namePHP图像上传和唯一文件名的分配
【发布时间】:2014-03-08 03:36:01
【问题描述】:

提前谢谢你。我检查了类似的问题,但它们没有帮助,因为工作流程的设置不同。 试图开始工作: 1.用户通过表单字段上传图片
2.(SCRIPT 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;
      }
  ?>

【问题讨论】:

    标签: php sql image upload uniqueidentifier


    【解决方案1】:

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

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

    【讨论】:

    • 这很有帮助,现在尝试弄清楚如何将 '132' 的值推送到 SQL 中,但它仍然没有出现在数据库中。我认为这将有助于我缩短我的问题并重新发布。 [insert_id] => [server_info] => [server_version] => [stat] => [sqlstate] => [protocol_version] => [thread_id] => [warning_count] => ) [10] => INSERT INTO cmets (姓名、电子邮件、评论、storyid、头像、entry_date)值('jgfhfgjhfg'、'fghjhfgh'、'jfghjfghjfghjfgj'、'132'、''、now()))
    • @buzrw 我在上面编辑了我的答案。请注意,“display_errors”必须设置为“1”而不是“0”。 '1' 将显示错误,而'0' 隐藏它们。此外,请确保将这些块包含在您要调试的所有文件中。此外,(这非常重要)如果您有一个网站并决定在开发过程中使用它们,那很好,但是一旦您的网站上线,您必须将它们注释掉或删除它们。否则,当发生错误时,您的查看器将看到每个错误的输出消息。本质上是突出您网站安全漏洞的路线图。恶意用户在桶中钓鱼。
    猜你喜欢
    • 2013-03-09
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多