View Full Version : Getting the filename of file uploaded

09-06-2011, 10:56 PM
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:

<form action="/index.php?page=home" enctype="multipart/form-data" method="post">
<table class="box">
<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." />
<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 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>
$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($filename, strpos($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.
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>';
echo "Error!";

09-07-2011, 12:19 AM
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?

$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


Go to that page in your browser and look for file_uploads and see if it is set to On or Off

09-07-2011, 12:28 AM
forgive me if am totally stupid but i cant see any mysql in that code . wouldnt you need that to insert it to database ???

09-07-2011, 12:31 AM
It located in another file.

09-07-2011, 12:34 AM
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.