SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Creating folders and files

    Hi,

    Im trying to setup a script to automatically setup a new directory and save a new image file that has been uploaded from a harddrive for the first time and am having a few probs.

    Im using the following:

    PHP Code:
    <?php
    require_once 'scripts/conn.php'

    // Where the file is going to be placed 

    $new_folder mkdir($_REQUEST['row_id'], 0777);

    $target_path $new_folder."/";

    /* Add the original filename to our target path.  
    Result is "uploads/filename.extension" */
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 
    $_FILES['uploadedfile']['tmp_name'];  

    $target_path $new_folder."/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

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

        
    $sql "INSERT INTO 
        wiltshire_listings
            (image_1)
            VALUES
            ('"
    basename$_FILES['uploadedfile']['name']) ."')
            "
    ;
            
            
    $results mysql_query ($sql)
                or die (
    "<br><br>Invalid query: " mysql_error());

    ?>
    Also, Is this the best way of saving images when a user uploads them?

    Thanks

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, what's the problem?

  3. #3
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oops sorry

    Warning: move_uploaded_file(1/26.jpg): failed to open stream: No such file or directory in d:\website\wiltshire\wiltshire_cms\upload_image_script.php on line 19

    Warning: move_uploaded_file(): Unable to move 'C:\PROGRA~1\EASYPH~1\\tmp\php210.tmp' to '1/26.jpg' in d:\website\wiltshire\wiltshire_cms\upload_image_script.php on line 19
    There was an error uploading the file, please try again!

    It is creating the folder at the beginning correctly. From the error message it seems to be looking for a folder called 1 but the actual folder i just created is something different e.g. 35/

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    print "$new_folder" What does it contain?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, just tried it their. It created the folder 62/ as it should have done but when i printed $new_folder it was set to 1

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    mkdir Returns TRUE on success or FALSE on failure.
    PHP Code:
    $newFolder $_REQUEST['row_id'] . '/';

    mkdir($newFolder); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If your users are only uploading images, a good way to validate the file is with getimagesize(), it will return true if it can get an image size which won't work for non-image files.

    Also you could double-check that the mkdir is setting the permission level to 777, I believe the setting that affects it in safe-mode? Something like that which won't allow you to create folders with 777, it'll be like 655.

  8. #8
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, got it working now


Bookmarks

Posting Permissions

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