Log in

View Full Version : Upload Form



pssparkman
10-31-2007, 07:30 AM
Hello,

I have an upload form that doesn't want to work correctly for some reason. I am new at this and have searched and searched, and found nothing that can help fix what I need. It keep coming up with the file type is not allowed, and the file I am trying to submit is in the list of allowed types.


<?php
// Configuration - Your Options
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.avi','.wmv',); // These will be the types of file that will pass the validation.
$max_filesize = 1500000; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file type you attempted to upload is not allowed.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['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.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload.'; // It failed :(.

?>

Twey
10-31-2007, 07:40 AM
Off-by-one error: you don't need to take away that one from strlen($filename). The first parameter of substr() doesn't include the actual character you specify, but the second does. It's kind of like instead of selecting a character, you're selecting the line between that character and the one before.

pssparkman
10-31-2007, 07:58 AM
Okay so do I remove the -1 or something else. Like I said before, I am new to this. If possible can you explain it better or show what it needs to look like?

Twey
10-31-2007, 11:40 AM
Yes, just remove the -1 and it should be fine.

pssparkman
10-31-2007, 01:38 PM
It still come up with the same error that the file is not allowed.

Twey
10-31-2007, 02:31 PM
Well, firstly you can just do:
$ext = substr($filename, strpos($filename,'.'));Secondly, echo $ext; and see what it says.

pssparkman
10-31-2007, 02:37 PM
Well, firstly you can just do:
$ext = substr($filename, strpos($filename,'.'));Secondly, echo $ext; and see what it says.

Where should I put the echo $ext;?

Twey
10-31-2007, 02:41 PM
Right after
$ext = substr($filename, strpos($filename,'.'));is fine.

pssparkman
10-31-2007, 03:11 PM
I put it after what you said and it still came up with the message of file not accepted.

Twey
10-31-2007, 03:20 PM
I suspect $_FILES['userfile']['name'] is empty. Did the file get uploaded correctly? Is the file input element named "userfile"? Did you set the enctype correctly on the form? Are you using POST?

boogyman
10-31-2007, 03:21 PM
he's telling you how to troubleshoot it.
above the file cannot be accepted it should print out the contents.


<?php
// Configuration - Your Options
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.avi','.wmv',); // These will be the types of file that will pass the validation.
$max_filesize = 1500000; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).

$ext = substr($filename, strpos($filename,'.'));
echo $ext;

pssparkman
10-31-2007, 04:46 PM
Well after some searching I found another upload script that works fine, just needs some tweaking to do what I need it to do but it works. On another note, how can I get the upload script to send out an email saying that something to the nature of

The file "file" has been uploaded to "directory" on "date".


Any help with this will be appreciated.

insanemonkey
11-01-2007, 06:32 PM
this is what i use,



<?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>


this one you can actually not allow certain file types... have fun

Twey
11-01-2007, 06:35 PM
It's a lot safer to only allow certain file types (e.g. you missed out .phtml there).

insanemonkey
11-01-2007, 06:44 PM
OMg there is a phtml, what is the world coming to, why do they have so many html things, all they need is html and htm ok!!!

geesh...

thanks for telling me..

is there a way i could turn this upload into just allowing certain file types, instead of me typing in everyone i dont want...

I cant seem to figure it out.... help plz

pssparkman
11-01-2007, 06:47 PM
I do however want to put in the form a way that when an upload is successful an email goes out to a specified address (listed inside the form) that a file has been uploaded. How can I approach this?

Twey
11-01-2007, 07:00 PM
is there a way i could turn this upload into just allowing certain file types, instead of me typing in everyone i dont want...Just take away the ! before the in_array() call. You might want to rename $naughtyFileTypes for clarity.
OMg there is a phtml, what is the world coming to, why do they have so many html things, all they need is html and htm ok!!!It's actually a PHP extension.

insanemonkey
11-01-2007, 07:14 PM
oooh more php, what i would do is have it uploaded

post it as get method like
http://www.yourdomain.com/uploaded/index.php?upload=$file

then have a email box below allow users to put in their email address then(like a contact form) when the users submit it sends them
$file


eek sorry confusing a litle

pssparkman
11-01-2007, 07:31 PM
Sorry, maybe I didn't explain it enough. I want to specify an email address inside the code that when the upload is successful it send an email to that user that "file name" has been uploaded to "dir" on "date". I don't need the user to submit an address for the email to go to. The address is already coded within the form.

insanemonkey
11-01-2007, 07:47 PM
use something like this. you have to use session in a way to identify the user..


$to = "$useremail";

if($submit)
{
mail("$uploadedfile");

} else {
echo "There was an error please try again";
}
?>
try this for like a process page

pssparkman
11-01-2007, 08:29 PM
So where you have mail("$uploadedfile"), that is where the message would be? Thanks.

insanemonkey
11-01-2007, 08:43 PM
yes..

mail("$uploadfileurl");

uploadfileurl is what the url is whats going to be send to them..
so they know where the file is.
or message..

pssparkman
11-01-2007, 08:48 PM
Thank You. Now one last question and I hope I can be done with this. I don't know anything about php, but from what others have helped me with. How do I incorporate it within the form so that when a file is successful it send this email?

insanemonkey
11-02-2007, 09:33 AM
I would make a process.php page. or same page.. its really hard, and took me for ever to make my host script that kinda does the same thing..

uhmm.. idea? well after it process your gonna wanna put the mail process rght after the code..

I can't do it right now but I can try tomorrow... not really sure if I can even do it..

pssparkman
11-03-2007, 05:49 AM
Need some help, I have been trying to understand the order of what goes where to get it to function properly, is this correct. It gives me errors, of course.



#-----------------------------------------------------------#
# this function will upload the files. :) ;) cool #
#-----------------------------------------------------------#
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
echo "File $i: ($file_name) has been uploaded successfully.";
}
$to = "pss_2002@hotmail.com";
if($submit)
{
mail("Email Message")
}else{
echo "File $i: Faild to upload.";
}#end of (move_uploaded_file).


Not sure if I really need the if($submit)???

pssparkman
11-06-2007, 01:03 AM
Okay, after some looking around on the net, I have found an script that contained the necessary parts for sending out emails and it works! Now the problem, the email doesn't come to me with line breaks in it as I have put them in using <br>. Is there a specific way or another piece of code that I am missing or need to us? I need to also know how to create links and a bulleted list in the email as well as my attempts with html code inside the email have failed.

pssparkman
11-09-2007, 08:48 PM
I need help with getting this to work in an upload form. I get it to where I can see the two radio buttons, but when I click on one, it doesn't want to show the rest of the form.

The initial setting is $num_files = 0 at the very begging of the script, as you select a radio button is suppose to show you the that amount of forms either it be 1 or 3.



echo " <input type=\"radio\" onclick= $num_files=3 name='1'>
<input type=\"radio\" onclick= $num_files=1 name='1'>
if ($num_files < 0){
echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
// show the file input field based on($num_files).
for ($i = 1; $i <= $num_files; $i++) {
echo "File $i: <input type=\"file\" size=\"70\" name=\"file". $i ."\"><br><br>";
}

pssparkman
11-11-2007, 02:39 AM
Okay, I got it to where when I click on what I want the selection is shown, but the issue that I have now is that all the available selections are capable of being seen. How can I hide those selections or call their function.

Shotgun Ninja
11-20-2007, 04:03 PM
When you guys get this done, let me know. I never even knew that files could be POSTed via a form.

pssparkman
11-20-2007, 06:55 PM
Take a look at the following: FTP Upload Form (http://www.dynamicdrive.com/forums/showthread.php?t=26422)

Shotgun Ninja
11-21-2007, 03:43 PM
Okay, after some looking around on the net, I have found an script that contained the necessary parts for sending out emails and it works! Now the problem, the email doesn't come to me with line breaks in it as I have put them in using <br>. Is there a specific way or another piece of code that I am missing or need to us? I need to also know how to create links and a bulleted list in the email as well as my attempts with html code inside the email have failed.


Use the wonderful function:

$goodText = br2nl("badText");

It might help.

Shotgun Ninja
11-21-2007, 03:43 PM
*owned*

boogyman
11-21-2007, 04:02 PM
Use the wonderful function:

$goodText = br2nl("badText");

It might help.

that is the abuse of the break-line tag.

anywhere he wants to have a new line insert the wildcard \n



$message = "This is \n some message.\n\t please refrain from doing that.";

would be displayed in the email as


This is
some message.
please refrain from doing that.

Twey
11-22-2007, 03:20 AM
that is the abuse of the break-line tag.It is. However, your proposed solution won't work since s/he evidently is sending HTML mail (also, \n is not a wildcard, it's an escape sequence: a wildcard is a character that matches a range of other characters, such as * in a DOS or bash). Instead, you need to wrap the offending text in a container with white-space: pre; like so:
$goodText = sprintf('<div style="white-space: pre;">%s</div>', $badText);