Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with an Upload.php file please

    Hey all,

    ive been having problems lately, trying to get a page working to upload images to a folder on the server. The code is this:

    <body>

    <div class='header'>
    Admin Managment
    </div>

    <div class='left'></div>
    <div class='right'></div>

    <div class='center'>

    <form action="" method="post" enctype="multipart/form-data"
    name="uploadImage" id="uploadImage">
    <p>
    <input type="hidden" name="MAX_FILE_SIZE"
    value="<?php echo MAX_FILE_SIZE; ?>" />
    <label for="image">Upload image:</label>
    <input type="file" name="image" id="image" />
    </p>
    <p>
    <input type="submit" name="upload" id="upload"
    value="Upload" />
    </p>
    </form>

    <?php
    // define a constant for the maximum upload size
    define ('MAX_FILE_SIZE', 1024 * 500);

    if (array_key_exists('upload', $_POST)) {
    // define constant for upload folder
    define('UPLOAD_DIR', '/images/');
    // replace any spaces in original filename with underscores
    $file = str_replace(' ', '_', $_FILES['image']['name']);
    // create an array of permitted MIME types
    $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
    'image/png');

    // upload if file is OK
    if (in_array($_FILES['image']['type'], $permitted)
    && $_FILES['image']['size'] > 0
    && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    switch($_FILES['image']['error']) {
    case 0:
    // check if a file of the same name has been uploaded
    if (!file_exists(UPLOAD_DIR . $file)) {
    // move the file to the upload folder and rename it
    $success =
    ----------> move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
    $file);
    } else {
    $result = 'A file of the same name already exists.';
    }
    if ($success) {
    $result = "$file uploaded successfully.";
    } else {
    $result = "Error uploading $file. Please try again.";
    }
    break;
    case 3:
    case 6:
    case 7:
    case 8:
    $result = "Error uploading $file. Please try again.";
    break;
    case 4:
    $result = "You didn't select a file to be uploaded.";
    }
    } else {
    $result = "$file is either too big or not an image.";
    }
    }
    ?>
    And these are the errors im getting when im trying to upload the image:

    Warning: move_uploaded_file(/images/GSK.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /clientdata/zeus-dynamic-1/k/a/karriheightsalpacas.com/www/Upload.php on line 84

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/phptmp/phpD6dt8r' to '/images/GSK.jpg' in /clientdata/zeus-dynamic-1/k/a/karriheightsalpacas.com/www/Upload.php on line 84
    Line 84 is marked with ------> it isn't actually in the code like that, Its just how I'm distinguishing it from the rest of the code.

    Ive made the folder read,write,execute for all in chmod, and im at whits end trying to figure out how to fix this.

    if anyone can help i would appreciate it a lot.

    Cheers,

    Karl Tysoe

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Change
    define('UPLOAD_DIR', '/images/');
    to
    PHP Code:
    define('UPLOAD_DIR'$_SERVER['DOCUMENT_ROOT']. '/images/'); 
    and try again.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Tiddles (11-08-2010)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,042
    Thanks
    15
    Thanked 240 Times in 240 Posts
    At least one other problem, the php code has to be above the form or MAX_FILE_SIZE is undefined, or it has to be defined above the form in a separate php tag. But there's definitely a path name problem too as abduraooft pointed out.

    PS: It works, after fixing the MAX_FILE_SIZE problem, making abduraooft's change and making a folder named images in the root of the server with the right permissions it works very well.
    Last edited by DrDOS; 11-08-2010 at 04:43 PM.

  • Users who have thanked DrDOS for this post:

    Tiddles (11-08-2010)

  • #4
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Cheers guys, Ive been working on that for a bit, was very frustrating.. don't know how many searches I've had for a fix, and didn't think about asking myself until tonight :/

    All works peachy now.

    Cheers,

    karl


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •