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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line