Jump to content

[SOLVED] how can i get the name an uploaded file (and the extension e.g. jpg)?


Recommended Posts

how can i get the name of an uploaded file,

this is my code

<?php
if(isset($_POST['upload']))
{
$target = "images/";
$image =  $_POST['uploaded'];
$imagename = $_POST['imagename'];
$imagecaption = $_POST['imagecaption'];
$user_check = mysql_num_rows(mysql_query("SELECT * FROM `hayleyimages` WHERE image_name = 1 "));
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1;

//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

if($user_check >= 1)
{
echo "$imagename already exists, please rename it.";
}

//If everything is ok we try to upload it
else
{
//if($image && $imagename && $imagecaption)
//{
mysql_query("INSERT INTO `hayleyimages` (image_link,  image_name, image_caption, image_id) VALUES 	  ( '$image','$imagename','$imagecaption', '')")
or die('Error ' . mysql_error());
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
} 
//}
//else
//{
//echo "<br>Please make sure all fields are filled in.";
//}
}
}
?>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">Image Name: <input type="text" name="imagename" /><br />Image Caption: <input type="text" name="imagecaption" /><br />
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Submit" name="upload" />
</form>

 

this

"The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

only echos

 

The file has been uploaded

 

 

It's $_FILES['uploaded']['name'] in your case. =P

 

 

Look:

Please choose a file: <input name="uploaded" type="file" /><br />

 

To get the extension:

$name = $_FILES['uploaded']['name'];

$extension = substr($name, strrpos($name, "."), strlen($name));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.