Moving uploaded files with 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.

Read More

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>

Related posts

Leave a Reply

2 comments

  1. The issue is with this line:

    move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
    

    You need to specify the uploaded directory in your second parameter, like so:

    move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir.$_FILES["file"]["name"]);
    
  2. 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 (80×80 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