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 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    122
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Getting the filename of file uploaded

    Hi guys. I'm making a file uploading codish thing. And I have a database right. And I want the name of file which is uploaded to go into my database. The problem is that when you upload it doesn't give the name of file rather leaving it blank. Here's part of the upload code:

    PHP Code:
    <form action="/index.php?page=home" enctype="multipart/form-data" method="post">
        <table class="box">
            <tbody>
                <tr>
    <fieldset>
                        <h2>Browse for your file:<em> required</em></h2>
                        <div class="file_input_div">
                          <input type="button" value="Browse&nbsp;&nbsp;»" class="file_input_button" />
                          <input type="file" name="site_url" class="file_input_hidden" onchange="javascript: document.getElementById('fileName').value = this.value" /></div>       
                        <input type="text" id="fileName" class="file_input_textbox" readonly="readonly" />
                        <img src="/images/info.png" class="tooltip" title="The URL to your site, eg: www.yoursite.com." />
    </fieldset>            
    </tr>
                <tr>
    <fieldset>
                        <h2>What are you uploading?<em> optional</em></h2>
                        <input id="name" name="site_description" size="30" type="text" class="input_textbox" value="" />
                        <img src="/images/info.png" class="tooltip" title="Tell us a little about what your uploading." />
                    </fieldset>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                                    <fieldset class="buttons">
                      <input type="hidden" name="form_key" id="form_key" value="<?php echo $_SESSION['form_key']; ?>" />
                      <button id="upload_button" name="submit" type="submit" value="Upload Now&nbsp;&nbsp;»">Upload Now&nbsp;&nbsp;»</button>
                    </fieldset>
                </tr>
            </tfoot>
        </table>
    </form>
    <?php
    $site_id 
    intval($_GET['id']);
    $site $sites->getSite($site_id);
       
    // Configuration - Your Options
          
    $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
          
    $max_filesize 524288// Maximum filesize in BYTES (currently 0.5MB).
          
    $upload_path './uploads/'// The place the files will be uploaded to (currently a 'files' directory).
     
       
    $filename $_FILES['site_url']['name']; // Get the name of the file (including file extension).
       
    $ext substr($filenamestrpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
     
       // Now check the filesize, if it is too large then DIE and inform the user.
       
    if(filesize($_FILES['site_url']['tmp_name']) > $max_filesize)
          die(
    'The file you attempted to upload is too large.');
     
       
    // Check if we can upload to the specified path, if not DIE and inform the user.
       
    if(!is_writable($upload_path))
          die(
    'You cannot upload to the specified directory, please CHMOD it to 777.');
     
       
    // Upload the file to your specified path.
       
    if(move_uploaded_file($_FILES['site_url']['tmp_name'],$upload_path $filename))
                      echo 
    'Sucessfully uploaded file. <a href="/index.php?page=site_view&id=' .$row['site_id'] .'">Here</a>';
    else
     echo 
    "Error!";
    ?>

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I assume you have tried echoing out the filename to the screen right after grabbing it and that is how you are seeing it is blank?

    PHP Code:
    $filename $_FILES['site_url']['name'];
    echo 
    $filename
    Have you checked to see if file uploads are enabled?

    Create a PHP file with nothing but the following in it

    PHP Code:
    <?php 
    phpinfo
    ()
    ?>
    Go to that page in your browser and look for file_uploads and see if it is set to On or Off
    Last edited by Spookster; 09-07-2011 at 01:22 AM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    Xaqa (09-07-2011)

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    forgive me if am totally stupid but i cant see any mysql in that code . wouldnt you need that to insert it to database ???

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    122
    Thanks
    23
    Thanked 0 Times in 0 Posts
    It located in another file.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I can confirm that your code does work but only if file_uploads in PHP is set to On. If I disable that option in PHP you will not get the file name. So my guess is wherever you are running the file_uploads setting is set to Off.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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