SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    php script: unable to upload files

    Hello,

    PHP file upload script.

    Problem: Unable to upload file under ubuntu server.

    I have 2 different webservers setup:
    1. xampp on usb and my problametic script works.
    2. ubuntu server, exact same php script but it wont upload the file.

    What i noticed with xampp: <--works
    echo "<br>tempFile=>:-". $_FILES['data']['tmp_name'][$x] . "<br>";
    result: tempFile=>:-G:\xampp\tmp\php2D.tmp
    print_r($imagearray);
    Array ( [0] => Array ( [1] => G:\xampp\tmp\php2D.tmp [2] => 1357153942_robinuser_dog.jpg ) )

    What i noticed with Ubuntu server:
    echo "<br>tempFile=>:-". $_FILES['data']['tmp_name'][$x] . "<br>";
    tempFile=>:/tmp/phpzN6e8U <-- looks like the "tmp" extension is missing.
    print_r($imagearray);
    Array ( [0] => Array ( [1] => /tmp/phpzN6e8U [2] => 1357153182_robinuser_bird.jpg ) )

    if the temp file ext is missing how do I fix this??

    Any help you can provide would be greatly appreciated

    PHP Code:
    ?PHP
    include('connection.php');
    ?>
    <html> 
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <!-- CSS -->
    <style type="text/css">
    div.color {
      border-width:2px;
      border-style:solid;
      border-color:#black;
      padding: 3 3 3 3px;
      width: 400px;  
      }
    </style>
      
      <!--  Java script -->
    <script>
    $(function(){
        $('#add-file-field').click(function(){
            $("#text").append('<div class="added-field"><input type="file" name="data[]"><input type="image" src="delete.png" class="remove-btn" value="Remove"></div>');
        });
        $('.remove-btn').live('click',function(){
            $(this).parent().remove();
        });
        });
    </script>
    </head>

    <?php
        $imageUserId 
    'robinuser';
            
        
    $allowedUpload 6;  // number of file the user can upload
        
    $allowedExts = array("jpg""jpeg""gif""png");
        
    $remove_these = array(' ','`','"','\'','\\','/');
        
    $upload_directory 'uploads/'//image upload folder
        
    $fileSize 20000;
        
    $imagearray=array();     
        
        if(isset(
    $_POST['btnsave']))
        {    for(
    $x=0$x<count($allowedUpload); $x++)
            {    echo 
    "<br>tempFile=>:-"$_FILES['data']['tmp_name'][$x] . "<br>";
                if(!empty(
    $_FILES['data']['name'][$x]))
                {    
    $extension end(explode("."$_FILES['data']['name'][$x]));
                    if (((
    $_FILES["data"]["type"][$x] == "image/gif")
                        || (
    $_FILES["data"]["type"][$x] == "image/jpeg")
                        || (
    $_FILES["data"]["type"][$x] == "image/png")
                        || (
    $_FILES["data"]["type"][$x] == "image/pjpeg"))
                        && (
    $_FILES["data"]["size"][$x] < $fileSize)
                        && 
    in_array($extension$allowedExts))
                    {    if (
    $_FILES["data"]["error"][$x] > 0)
                        {    echo 
    "Error: " $_FILES["data"]["error"][$x] . "<br>"; }
                        else
                        {    
    //Sanitize the filename 
                            
    $sanitizedName str_replace($remove_these''$_FILES['data']['name'][$x]);
                            
    $newImageName time()."_".$imageUserId."_".$sanitizedName;
                            
    //move_uploaded_file($_FILES['data']['tmp_name'][$x], $upload_directory . $newImageName);
                            
    if(!empty($newImageName))
                            {    
    $imagearray[$x][1] = $_FILES['data']['tmp_name'][$x];
                                
    $imagearray[$x][2] = $newImageName;
                            }                                
                        }
                    }
                    else
                    {    echo 
    "Invalid file: ".$_FILES["data"]["name"][$x];  }
                }
    // end of IF- if(!empty($_FILES['data']['name'][$x]))
            
    // endo for loop
            
    print_r($imagearray);
            for(
    $y=0$ycount($imagearray); $y++)
            {    
    move_uploaded_file($imagearray[$y][1], $upload_directory $imagearray[$y][2]);    
                echo 
    $imagearray[$y][1] . " " .$upload_directory $imagearray[$y][2]."<br>";
            }                  
        } 
    // end of upload button
    ?>
    <body>
    <form enctype="multipart/form-data" action="" method="POST">
           <div class="color">
        Upload:
        <br> - Excepted file formats: ing: JPEG, PNG, & GIF formats.
        <br> - Max number of files you can upload: 6
        <hr>
            <div id="text">
                <div ><input name="data[]" id="file" type="file" multiple="multiple" /></div>
            </div>
            <img src="add.jpg"  id="add-file-field" name="add" style="margin-top:21px;"/> 
            <input type="submit" name="btnsave" value="Upload File" />
        </div>
    </form>
    </body>
    </html>

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Have you checked the read and write permissions in the destination folder?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The filename of those temporary files should not matter at all to your script. Why do you care so much that the extension is missing? As far as I can see your script would work just fine, and if it doesn't I can assure you the problem does not lie with the missing extension.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Could you put var_dump($_FILES['data']) and give us the output? I believe you have an error with the upload process, and there is data within the array that will help us identify the problem.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    157
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Could you put var_dump($_FILES['data']) and give us the output? I believe you have an error with the upload process, and there is data within the array that will help us identify the problem.
    Hello

    var_dump($_FILES['data']); results:

    array(5) { ["name"]=> array(1) { [0]=> string(8) "bird.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpymaAFB" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(5537) } }

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Okay, so there isn't an error with the file that is being uploaded (such as size limit). Now I'd recommend turning on error reporting to see if any other errors are being created/hidden.

    PHP Code:
    ini_set("error_reporting"E_ALL E_NOTICE); 
    ini_set("display_errors"1); 


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
  •