View Full Version : PHP File Upload Script
Rockonmetal
11-06-2007, 04:51 PM
Hey I need a piece of PHP code that will help my user's upload their files. I need to have it in a function or something that i can do this:
if($secret==somenumber){
uploadthatfilenow();
}{
echo "Please fill refill out the form...";
}
I do not need a file upload form... I just need a piece of code that can upload the file, check to make sure it is either a jpg, gif, mov, flv, png, jpeg...
I also need for the file to be renamed so that its the next number...
Example below...
A user uploads a file named "funny.jpg" it gets renamed to "1.jpg"
Another user later uploads a file named "JOKE.mov" 1.mov
Another user uploads a file named "FUNNY.jpg" it gets renamed to "2.jpg"
Basically I think the above is a system of make the filename into 1 and then checking to see if that file name 1 is taken and then increase the number...
I also need the file to be saved into a folder called "Pending"...
This is one area I can't quite get... hopefully if someone can really explain how todo this or better yet just give me the code and so that I can look at it and make sense. Thanks to those who help
boogyman
11-06-2007, 05:20 PM
building the upload script is really kinda heavily dependant on the data being sent to it... you are asking for a custom script, but telling us to create a generic script.
we helped you build a script before and there are multiple other threads here on upload scripts. If you change your script from before into a function you could very easily modify it to your specifications.
now on the folder / file name concept. I would suggest making a folder called pending, and then sub folders for each format that you wish to support. this will help separate between the different file types that you are allowing.
insanemonkey
11-06-2007, 05:30 PM
here you go, try this out..
<?php
error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.
// Check to see if the button has been pressed
if (!empty($_REQUEST['sendForm']))
{
// Assign the name to a variable
$name = $_FILES['uploaded_file']['name'];
// Assign the tmp_name to a variable
$tmp_name = $_FILES['uploaded_file']['tmp_name'];
// Assign the error to a variable
$error = $_FILES['uploaded_file']['error'];
// Assign the size to a variable
$size = $_FILES['uploaded_file']['size'];
// No trailing slash
$uploadFilesTo = 'imgs';
// Create safe filename
$name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
// Disallowed file extensions
$naughtyFileExtension = array("php2", "php3", "php4", "php5", "asp", "inc", "txt", "wma", "mov", "js", "exe", "jsp", "map", "obj", " ", "", "html", "mp3", "mpu", "wav", "cur", "ani", "fla", "swf");
// Returns an array that includes the extension
$fileInfo = pathinfo($name);
// Check extension
if (!in_array($fileInfo['extension'], $naughtyFileExtension))
{
// Get filename
$name = getNonExistingFilename($uploadFilesTo, $name);
// Upload the file
if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
{
// Show success message
echo '<center><a href="http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'" target="_blank">
<img src="http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'" align="top" width="100" border="0"></a>
<textarea>http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'</textarea></center>';
}
else
{
// Show failure message
echo '<center><SPAN style="color:gray">File failed to upload to /'.$name.'</span></center>';
}
}
else
{
// Bad File type
echo '<center><SPAN style="color:gray">The file uses an extension we don\'t allow.</span></center>';
}
}
// Functions do not need to be inline with the rest of the code
function getNonExistingFilename($uploadFilesTo, $name)
{
if (!file_exists($uploadFilesTo . '/' . $name))
return $name;
return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
}
?>
<BR>
<center>
<table bgcolor="#000000" border="0" width="100%">
<tr><td>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" name="sendForm" value="Upload">
</form>
</td></tr>
</table>
</center>
http://forms5.createforms.com/33131/form_1_1.htmlA blank grey page?
insanemonkey, you're still using that bad upload script?
shriniwas
12-28-2007, 03:50 AM
Hi, I'm using this code for file upload....
its fine with images but how can i upload other formats such as Microsoft Word or Exel file or any simple text file
Can anyone help me ??
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if
(file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
codeexploiter
12-28-2007, 04:06 AM
Check the following list
"application/pdf" - PDF Files
"application/msword" - MS Word Files
"application/powerpoint" - MS Powerpoint Files
"application/excel" - MS Excel Files
"text/plain" - Text Files
Hope this helps
shriniwas
12-28-2007, 03:38 PM
Thanks alot !!!! :):)
It worked very well ....
Haven't tried all formats but simple text is working and has solved my problem..... :)
Thanks again...
shriniwas
01-03-2008, 05:41 PM
Hi,
i have 2 servers.
I foubd that my server 2 doesn't supports Php :mad: so is there any other way :confused:
like asp solution for it ??
(recomending asp b'coz its supported by my server, i have only problem with .net and php anything else will do :o)
Shriniwas
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.