Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Image Upload (WordPress Custom Plug-in)

I have a WordPress plug in that I am working on and I seem to have hit a problem. I need the user to be able to upload an image, instead I keep getting this problem:

Upload directory is not writable, or does not exist.

Warning: move_uploaded_file(http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in /home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php on line 39


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpowqMlu' to 'http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png' in /home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php on line 39 There was an error uploading the file, please try again! You have successfully added test_title to your products list


The directory definitely exists and I even changed the CHMOD to 777 for a test.

The products.php file looks like this:

 <?
if ('POST' == $_SERVER['REQUEST_METHOD'])
 {
global $wpdb;

$product_title = $_POST['title'];
$product_url = $_POST['url'];
$product_btn_text = $_POST['btn_text'];

// CREATE UNIQUE NAME FOR IMAGE
$remote_addr = $_SERVER['REMOTE_ADDR'];
$time = time();
$new_name = $remote_addr;
$new_name .= $time;


// IMAGE UPLOAD

$upload_dir = "http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/"; 

if (file_exists($upload_dir) && is_writable($upload_dir)) {
        echo "<br /> Directory exists and is fine.... <br />";
}
else {
        echo "Upload directory is not writable, or does not exist. <br />";
}

$uploadedfile = $_FILES['image_file']['name'];
$extension = explode(".", $uploadedfile);
$extensiontype = $extension['1'];

$target_path = $upload_dir;
$target_path = $target_path .$new_name .'.'.$extensiontype; 

if(move_uploaded_file($_FILES['image_file']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['image_file']['name']). 
        " has been uploaded <br />";
} else{
    echo "There was an error uploading the file, please try again! <br />";

}

$product_img = $new_name.'.'.$extensiontype;

//ADD TO DATABASE
    $wpdb->query("INSERT INTO wp_rec_amazon_product (product_title, product_url,    product_img, product_btn_text) VALUES ('$product_title', '$product_url','$product_img','$product_btn_text')");
echo "You have successfully added "  .$product_title.   " to your products list";

} else { ?>

 <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<table border="0" bordercolor="none" width="50%" cellpadding="0" cellspacing="0">
    <tr>
        <td>Product Title:</td>
        <td><input type="text" name="title" value="product title" /></td>
    </tr>
    <tr>
        <td>Product Url</td>
        <td><input type="text" name="url" value="product url" /></td>
    </tr>
    <tr>
        <td>Button text</td>
        <td><input type="text" name="btn_text" value="Get your copy" /></td>
    </tr>

</table>
    <input type="file" name="image_file" />



    <input type="submit" value="Submit" />
</form>

 <?php }?>

The information gets added to the databse, its just the image upload that does not seem to be working.

Has anyone got any ideas? thank you all in advance. :)

like image 690
Danienllxxox Avatar asked Mar 23 '26 23:03

Danienllxxox


1 Answers

Like @thenetimp said!

The destination of move_uploaded_file cannot be a URL. But instead has to be a path on the local file system.

$upload_dir has to point to a local path like

windows: c:/some/path
unix: /some/path
like image 155
tlenss Avatar answered Mar 26 '26 11:03

tlenss