El script es el siguiente, y como he dicho, a veces funciona bien:
Herni
Código PHP:
<?php
/*
---Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.
So if I uploaded the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/
//**********************************************************************//
// $_FILES['filetoupload'] is the value of //
// file field from the form. <input type="file" name="filetoupload"> //
//**********************************************************************//
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "picture_library/"; //change to whatever you want.
//204800 bytes = 200KB
$size_bytes = 204800; //File Size in bytes (change this value to fit your need)
$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
$limitedext = array(".jpg",".jpeg"); //Extensions you want files uploaded limited to. also you can use: //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");
//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
}
if($uploadform) // if you clicked the (Upload File) button. "If you submitted the form" then upload the file.
{//begin of if($uploadform).
//check if no file selected.
if (!is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{
echo "Error: Please select a file to upload!. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't do anything else.
}
//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that file size is correct
if ($size > $size_bytes)
{
$kb = $size_bytes / 2048;
echo "File Too Large. File must be <b>$kb</b> KB. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//check file extension
$ext = strrchr($_FILES['filetoupload'][name],'.');
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo("Wrong file extension. ");
exit();
}
// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['filetoupload']['name'];
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
echo "Oops! The file named <b>$filename </b>already exists. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//Move the File to the Directory of your choice
//move_uploaded_file('filename','destination') Moves afile to a new location.
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {
//tell the user that the file has been uploaded and make him alink.
echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded! <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
// print error if there was a problem moving file.
else
{
//Print error msg.
echo "There was a problem moving your file. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
}//end of if($uploadform).
#---------------------------------------------------------------------------------#
// If the form has not been submitted, display it!
else
{//begin of else
?>
<style type="text/css">
<!--
.Estilo1 {
font-family: Arial, Helvetica, sans-serif;
font-size: x-small;
font-weight: bold;
}
.Estilo2 {
font-family: Arial, Helvetica, sans-serif;
font-size: xx-small;
}
.Estilo3 {
font-family: Arial, Helvetica, sans-serif;
font-size: x-small;
}
-->
</style>
<br>
<h3 align="center" class="Estilo1">Browse a File to Upload:</h3>
<div align="center" class="Estilo3"><i>- Allowed Extensions:</i>
<b>
<?
// print the file extensions
for($i=0;$i<count($limitedext);$i++)
{
if (($i<>count($limitedext)-1))$commas=", ";else $commas="";
list($key,$value)=each($limitedext);
echo $value.$commas;
}
?>
</b>
<br>
<i>- Max File Size</i> = <b><?echo $size_bytes / 2048; ?> KB </b>
<br>
</div>
<form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>">
<div align="center"><br>
<span class="Estilo3">Photo</span>:
<input type="file" name="filetoupload">
<br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<input type="Submit" name="uploadform" value="Upload">
</div>
</form>
<?
}//end of else
/*______________________________________________________________________________*/
// Here is the most interesting part.
// it views the directory contents.....i'll disscuss next version. (ver 2.0)
?>
<hr>
<p align="center"><a href="frmes.html"><img src="../img/02_80.png" width="80" height="38" border="0"></a></p>
<p align="center"><font size="2" face="Arial, Helvetica, sans-serif">©</font><font size="1" face="Arial, Helvetica, sans-serif"> 2005 Hidesigns. </font></p>
<p align="center"><font color="#666666" size="1" face="Arial, Helvetica, sans-serif">zerotwo, zer02 & the 02 logo are <font size="2">®</font> registered trademarks of Hidesigns.</font></p>
<p align="center"><font color="#666666" size="1" face="Arial, Helvetica, sans-serif">Reproduction, distribution, display, or transmission of the content of this web site, including texts, graphics, images, and code is strictly prohibited without authorization.</font></p>
<hr>