【问题标题】:Upload image and retrieving in server WORDPRESS上传图片并在服务器 WORDPRESS 中检索
【发布时间】:2014-08-08 04:58:10
【问题描述】:

我正在尝试在服务器中上传图像并进行检索。
我做了一些事情,但问题是图像没有上传。我的脚本只会获取文件名,然后图像将保存在服务器文件夹中。这个脚本只保存了其他数据,不保存图片,我不知道哪里出错了,但是我使用的路径是正确的,我还需要检索图片并将其发布到前端。

 <div class="wrap">
              <form method="POST">
                    Date: <input type="text" name="class_date"> <br>
                    Event: <input type="text" name="class_name"><br>
                    <input type="hidden" value="class_sched" name="posttitle"><br>

                    <input type="hidden" name="size" value="350000">
                    Image:<input type="file" name="class_image"><br> 
                    <input type="submit" value="submit" name="submit">
              </form>  

        </div>      

    <?php

    global $wpdb;

        if(isset($_POST['submit']))
        {

            $target = plugins_url("/images",__FILE__);
            $target = $target . basename( $_FILES['class_image']['name']);

            $class_date = $_POST['class_date'];
            $class_name = $_POST['class_name'];
            $class_image = ($_FILES['class_image']['name']);

            $wpdb->insert('wp_calendar', array('class_name'=> $class_name, 'class_date'=>$class_date, 'class_image'=>$class_image));

             //Writes the photo to the server
            if(move_uploaded_file($_FILES['class_image']['tmp_name'], $target))
            {

                //Tells you if its all ok
                echo "The file ". basename( $_FILES['class_image']['name']). " has been uploaded, and your information has been added to the directory";
            }
            else 
            {

                //Gives and error if its not
                echo "Sorry, there was a problem uploading your file.";
            }       

        }

【问题讨论】:

  • 您确定要写入图像的文件夹具有 777 写入权限吗? (在 ftp 中。您可以使用 filezilla 等程序进行检查)
  • 基本上,。这是工作,。但上传的图片不在目录中。

标签: php mysql wordpress image upload


【解决方案1】:

添加 move_uploaded_file 功能。在上面的代码中,只检查 move_uploaded_file 是否为真。因为是if语句。

在 $wpdb->insert('wp_calendar', .... 之后添加以下代码。

move_uploaded_file($_FILES['class_image']['tmp_name'];

move_uploaded_file 函数将文件从临时文件夹移动到插件/图像文件夹。

【讨论】:

    猜你喜欢
    • 2014-09-03
    • 2014-07-16
    • 1970-01-01
    • 2015-11-07
    • 2011-06-01
    相关资源
    最近更新 更多