dpthuyh1678 2014-01-27 18:10
浏览 43
已采纳

使用php移动上传的文件

I am having issues moving files once uploaded. The upload appears to pass ok with no errors reported. I have 777 on the folder to upload to. The system is wordpress and I have no idea what I am doing wrong.

It should be noted that the form is inside another form. The eventual outcome is to have an image upload (this form, which is inside the larger one) that will allow the user to crop the image and add tags, title description etc before submitting the second form. The final submission of the second form will post to a custom post type and that is working fine. just the moving files and jcrop I am concerned about.

can anyone see a typo in there?

I cannot.

<form method="POST" action="" enctype="multipart/form-data">
                <label for="image_upload">Image Upload</label>
                <input id="image_upload" type="file" class="text_input" value="" name="file">
                <input id="image-upload" type="submit" class="button" value="Upload" name="upload">

                <!-- <img id="image-upload" src="<?php echo get_template_directory_uri(); ?>/images/sago.jpg" alt=""> -->

                    <?php 
                    // Process the upload 
                        if (!empty ($_POST['upload'])) {

                            $allowedExts = array("gif", "jpeg", "jpg", "png");
                            $temp = explode(".", $_FILES["file"]["name"]);
                            $extension = end($temp);
                            if ((($_FILES["file"]["type"] == "image/gif")
                            || ($_FILES["file"]["type"] == "image/jpeg")
                            || ($_FILES["file"]["type"] == "image/jpg")
                            || ($_FILES["file"]["type"] == "image/pjpeg")
                            || ($_FILES["file"]["type"] == "image/x-png")
                            || ($_FILES["file"]["type"] == "image/png"))
                            && ($_FILES["file"]["size"] < 100000)
                            && in_array($extension, $allowedExts))
                              {
                              if ($_FILES["file"]["error"] > 0)
                                {
                                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                                }
                              else
                                {
                                echo "<div> Upload: " . $_FILES["file"]["name"] . "<br>";
                                echo "Type: " . $_FILES["file"]["type"] . "<br>";
                                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br> </div>";

                                //set temp dir path
                                $path = $_SERVER['DOCUMENT_ROOT'];
                                $upload_dir = $path . '/wp-content/uploads/jcrop_temp/';    

                                if (file_exists($path . '/wp-content/uploads/jcrop_temp/' . $_FILES["file"]["name"]))
                                  {
                                  echo "<div style='border: solid 1px #BF5738; color: #BF5738; padding: 1em;'> The File: <span style='color: black;'>" . $_FILES["file"]["name"] . "</span> already exists. Please rename the file before trying again. </div>";
                                  }
                                else
                                  {
                                  move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
                                  echo "Stored in: " . $upload_dir . $_FILES["file"]["name"];
                                  echo "<div style='border:solid 1px #E1E1E1; max-width: 710px; text-align: center;'>
                                            <img id='image-upload' src='" . "/wp-content/uploads/jcrop_temp/" . $_FILES["file"]["name"] . "'>
                                        </div>
                                        ";
                                  }
                                }
                              }
                            else
                              {
                              echo "Invalid file";
                              }

                            //end upolad if  
                            }

                    ?>
            </form>
  • 写回答

2条回答 默认 最新

  • dongtan6336 2014-01-31 10:50
    关注

    OK, So it is a convoluted process. Simple enough outside of Wordpress but inside.... its a pain.

    There were a few thing I needed to change, firstly the file size was in bytes not kb! Idiot!...(Thanks to Panama Jack for making me look at it again and reminding me to not assume things.)

    Secondly, the move_uploaded_file() function does NOT work inside wordpress. Instead I cobbled together something out of this useful post:http://wordpress.org/support/topic/using-move_uploaded_file-in-a-plugin

    $path_array  = wp_upload_dir();
            $path = str_replace('\\', '/', $path_array['path']);
            $old_name = $_FILES["image_upload_path"]["name"];
            $split_name = explode('.',$old_name);
            $time = time();
            $file_name = $time.".".$split_name[1];
            move_uploaded_file($_FILES["image_upload_path"]["tmp_name"],$path. "/" . $file_name);
    

    (Please note if you use this code you WILL need to know what you are doing as the references in the question and answer do not correlate.)

    With this I was able to send the uploaded file to the uploads directory and generate the various image sizes that wordpress likes (80x80 thumb, medium, large etc).

    Why WP doesnt allow move_uploaded_file is beyond be....anyone?

    Either way, it is possible, just a pain. I hope this helps.

    Other Resources I used to get it working: http://cube3x.com/2013/03/upload-files-to-wordpress-media-library-using-php/

    move_uploaded_file() wordpress plugin

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化