File Upload Code

Here is my problem. I want to create a file upload link on my website. I have secure login for users, so security is not my concern here. What I would like to do is have a “Upload Videos Here” link that opens a popup window. In that popup window have the fields: Name, Comments, and File to Upload. I can create a simple form and do this. My problem starts when I want to save the file to the server(say in ./uploads/) and also want to send an email sent to me with the Name of the person submitting the video and their comments. Help?

PHP - Upload Code:
[php]// Get file path and size
$targetfolder = ‘…/…/images/uploads/’;
$targetfolder = $targetfolder.basename($_FILES[‘filename’][‘name’]);
$filesize = basename($_FILES[‘filename’][‘size’]);

// Check file size
if ($filesize > 26214400 )
{ // Error
echo ‘The maxiumum file size is 26214400 bytes (25MB),
’;
echo ‘Your file is $filesize bytes!’;
}
else // Successful
{
if(move_uploaded_file($_FILES[‘filename’][‘tmp_name’], $targetfolder)) // Move file to $targetfolder
{
echo 'The file ‘; // Confirmation message
echo basename($_FILES[‘filename’][‘name’]);
echo ’ has been successfully uploaded!’;
}
else // No file selected
{
echo ‘Sorry, a problem was encountered.’;
}
}[/php]

HTML - Form Code

[code]



* Maxiumum file size is 26214400 bytes (25MB)


[/code]

You can find the email code by looking in tutorials on youtube and adding the sendemail.php (or whatever you name it) to upload.php via include.

  • Miiikkeee

Here is my code for you. If you could take a look at this and let me know what is going wrong. The goal is for the user to enter their name, comment and file to upload. On submission, move the file to the /uploads/ folder and send me an email with the subject “A video submission” and the message “‘their name’ has submitted a video for approval.” followed by the comments they entered.

I keep getting this error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /…/…/public_html/upload.php on line 20

First is uploadform.php and second is upload.php.

uploadform.php
[php]<?php
require_once(‘auth.php’);
?>
{
width: 600px;
height: 420px;
margin: 0 auto;
position: relative;
clear: left;
}

body { margin: 0; padding: 0; background-color: #000032; color: #D2D2D2; } a:hover { color: #90F518; }

File to Upload 25 MB Max
Comments
PSN ID
[/php]

upload.php
[php]<?php
// Get file path and size
$targetfolder = ‘./uploads/’;
$targetfolder = $targetfolder.basename($_FILES[‘Fileupload’][‘name’]);
$filesize = basename($_FILES[‘FileUpload’][‘size’]);

// Check file size
if ($filesize > 26214400 )
{ // Error
header(“Location: http://www.mydomain.com/uploaderror.php/”);
exit;
}
else // Successful
{
if(move_uploaded_file($_FILES[‘FileUpload’][‘tmp_name’], $targetfolder)) // Move file to $targetfolder
{
$to="[email protected]";
$subject=“Domain Video Submission”;
$name=$_REQUEST[‘PSNID’];
$message=$name" has submitted a video for approval, stating ".$_REQUEST[‘Comment’].;
$header="From: " $name;
$sent=mail($to, $subject, $message, $header);

		header("Location:  http://www.mydomain.com/uploadsuccess.php/");
		exit;
	  }
	  else // Error
	  {
		header("Location:  http://www.mydomain.com/uploaderror.php/");
		exit;
	  }
	}

?>

[/php]

In upload.php, on line 20, you have :
[php]".$_REQUEST[‘Comment’].;[/php]
The . before the semi colon is whats causing your error.

  • Miiikkeee

Also, with PHP the $_REQUEST method is generally deemed unsafe as people can pass the variables by POST or GET. As you probably know, using GET could possibly make your site vulnerable to SQL Injection. Just something to consider :slight_smile:

  • Miiikkeee

Instead of doing all the stuff I was trying, I decided to make a simple form and upload using miiikkeee recommendations. The only thing I added was the authentication I needed to make sure random people are not submitting garbage. The only thing I would like to add is to the upload.php file. I would like to add an event onClose that will send a generic email to me that says someone uploaded a file.

Give me 5 min, and I’ll build you the code

  • Miiikkeee

Nevermind. I just added this and it works perfectly.
[php]$To = ‘[email protected]’;
$Subject = ‘mydomain.com Video Uploaded’;
$Message = ‘A video has been submitted for approval and possible addition to the website.’;
$Headers = ‘From: [email protected] \r\n’ .
‘Content-type: text/html; charset=UTF-8 \r\n’;

mail($To, $Subject, $Message, $Headers);
[/php]
Thank you very much.

Okay, glad you got it working!

  • Miiikkeee
Sponsor our Newsletter | Privacy Policy | Terms of Service