View Full Version : Need to create from with upload ability
melgem 07-16-2009, 11:47 PM I have been doing web editing and a little design as a volunteer for a few years. My experience is limited so I'm unsure what the lingo is. I need to be able to allow those filling out a form I've created to upload a document that would come through with the old field information once the form is submitted.
Thanks.
I think you can download these php mailers for free:
http://phpmailer.worxware.com/
iGeek 07-17-2009, 01:57 AM I think she wants to upload a document/file to her webserver. Is that correct? Your post is a little bit unclear.
melgem 07-17-2009, 04:31 PM I'm creating a form on a website that I edit and do a little design for and they want those filling out the form to be able to upload/attach to it a Word document or pdf as on the fields and then submit it. Im not sure how to do this. I hope this is more clear.
Thanks.
melgem 07-17-2009, 04:35 PM Another thing. I work with FTP and this form is already created so I have the ability to submit forms to email but not sure about the attaching/uploading documents to the form to be submitted.
iGeek 07-17-2009, 06:07 PM Use PHP for this.
<form action="nameofphpfilebelow.php" method="post">
<label for="file">Upload file (PDF/DOC):</label> <input type="file" name="file" id="file" size="40" />
<input type="submit" name="upload_submit" value="Upload File" />
</form>
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
if( (isset($_POST['upload_submit'])) )
{
if( (!empty($_FILES['file'])) && ($_FILES['file']['error'] == 0) )
{
$filename = basename($_FILES['file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if( $ext == 'pdf' || $ext == 'doc' )
{
$currentpath = pathinfo($_SERVER['SCRIPT_FILENAME'],PATHINFO_DIRNAME);
$newname = './uploads/'.$filename;
if ( !file_exists($newname) )
{
if ( (move_uploaded_file($_FILES['file']['tmp_name'],$newname)) )
{
echo 'File uploaded successfully.';
}
else
{
die('Error saving file.');
}
}
else
{
die('File already exists.');
}
}
else
{
die('Invalid file extension.');
}
}
else
{
die('File empty or corrupted.');
}
}
}
String in $newname before the filename is concatenated is the upload path.
Joseph Witchard 07-18-2009, 09:53 PM $ext = substr($filename, strrpos($filename, '.') + 1);
Not that this is my thread, but what exactly does that bit do?
iGeek 07-18-2009, 10:33 PM substr() returns part of a string. The syntax is substr(string string, int start, [int length]). strrpos() returns the position (int) of the last occurrence of a string in a string (case-sensitive, insensitive is strripos()). The syntax is strrpos(string haystack, string needle, [int offset]).
To find the file extension ($ext) we want to return just the file extension:
$ext = substr(
$filename, /** Full path (filename) **/
strrpos($filename, '.') + 1 /** Returns the position of '.'
* Since we don't want the period
* (just the extension) we add 1
* to return the position right after
**/
);
So substr now returns the part of the string following the period - the file extension. :)
Joseph Witchard 07-19-2009, 04:47 AM All right, thanks. I didn't really understand why you were adding + 1 to strrpos:)
zeeshan_2011 04-14-2010, 07:47 AM you can upload this easily.
nikole95 01-17-2011, 10:12 AM Use PHP for this.
<form action="nameofphpfilebelow.php" method="post">
<label for="file">Upload file (PDF/DOC):</label> <input type="file" name="file" id="file" size="40" />
<input type="submit" name="upload_submit" value="Upload File" />
</form>
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
if( (isset($_POST['upload_submit'])) )
{
if( (!empty($_FILES['file'])) && ($_FILES['file']['error'] == 0) )
{
$filename = basename($_FILES['file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if( $ext == 'pdf' || $ext == 'doc' )
{
$currentpath = pathinfo($_SERVER['SCRIPT_FILENAME'],PATHINFO_DIRNAME);
$newname = './uploads/'.$filename;
if ( !file_exists($newname) )
{
if ( (move_uploaded_file($_FILES['file']['tmp_name'],$newname)) )
{
echo 'File uploaded successfully.';
}
else
{
die('Error saving file.');
}
}
else
{
die('File already exists.');
}
}
else
{
die('Invalid file extension.');
}
}
else
{
die('File empty or corrupted.');
}
}
}
String in $newname before the filename is concatenated is the upload path.
The above is an example of the MySQL Improved extension used by PHP. When you see a query like this, don't immediately assume that it's a syntax error
______________________
wii modchip (http://www.youtube.com/watch?v=zjsfE4FR33Y)
wii mod chip (http://www.youtube.com/watch?v=zjsfE4FR33Y)
nikole95 01-17-2011, 10:13 AM Use PHP for this.
<form action="nameofphpfilebelow.php" method="post">
<label for="file">Upload file (PDF/DOC):</label> <input type="file" name="file" id="file" size="40" />
<input type="submit" name="upload_submit" value="Upload File" />
</form>
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
if( (isset($_POST['upload_submit'])) )
{
if( (!empty($_FILES['file'])) && ($_FILES['file']['error'] == 0) )
{
$filename = basename($_FILES['file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if( $ext == 'pdf' || $ext == 'doc' )
{
$currentpath = pathinfo($_SERVER['SCRIPT_FILENAME'],PATHINFO_DIRNAME);
$newname = './uploads/'.$filename;
if ( !file_exists($newname) )
{
if ( (move_uploaded_file($_FILES['file']['tmp_name'],$newname)) )
{
echo 'File uploaded successfully.';
}
else
{
die('Error saving file.');
}
}
else
{
die('File already exists.');
}
}
else
{
die('Invalid file extension.');
}
}
else
{
die('File empty or corrupted.');
}
}
}
String in $newname before the filename is concatenated is the upload path.
The above is an example of the MySQL Improved extension used by PHP. When you see a query like this, don't immediately assume that it's a syntax error
______________________
wii modchip (http://www.youtube.com/watch?v=zjsfE4FR33Y)
wii mod chip (http://www.youtube.com/watch?v=zjsfE4FR33Y)
|