Problems with a file upload script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Problems with a file upload script

Post by aredden »

Hey, working on a file upload script, and I have a basic one working fine. I just want to add a bit of security to it so that people I work with dont accidently upload useless files to our server. Here is what I have so far.

But it wont work. And I can't figure out why.


Edited out code due to repost below.
Last edited by aredden on Tue May 29, 2007 11:07 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, there's no closing bracket...? That could just be the snippet though.

Anyway, when does the problem occur in the code?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

When you say
But it wont work.
what exactly do you mean?
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Post by aredden »

Ok, so if i have this i get no error messages. but at the same time I dont think the move_upload_file is working out properly.

Code: Select all

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if($_FILES['uploadedfile']['type'] == "text/csv")
{
	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
	 {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).  " has been uploaded. ";
    echo ('<a href="main.php" target="_parent">Click Here</a> to enter Fare Quote.');
  }
  else
  {
    echo "There was an error uploading the file, please try again!";
  }
}
?>
this is driving me mad, everything to me looks right remove the if and the ( ) around move_upload_file and get a "Parse error: parse error, unexpected '{' in /home/content/f/m/a/fmafadmin/html/Response/uploader.php on line 17" which i understand because { are for if contatining commands with an if statement. so at the moment im a bit stuck with this. :S
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Show your form please.

On second thought, try this for starters...

Code: Select all

<?php
$target_path = "uploads/";
$target_path .= basename( $_FILES['uploadedfile']['name']); // Why are you doing this?

echo '<-- Upload File Type is ' . $_FILES['uploadedfile']['type'] . ' ... -->';
//if ($_FILES['uploadedfile']['type'] == "text/csv")
//{
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).  " has been uploaded. ";
        echo ('<a href="main.php" target="_parent">Click Here</a> to enter Fare Quote.');
    }
    else
    {
        echo "There was an error uploading the file, please try again!";
    }
//}
?>
Then after you run it once to upload the file, view the source to see what the file type. I suspect that the conditional is for some reason evaluating to false so there is no upload.
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Post by aredden »

got it working. but now I have another question im researching. Is it possible to create a file and email it attached to a message all in one script?

if so what am I looking for. a pause or wait function?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would take a look at Swiftmailer written by our very own d11wtq. It has support for mailing attachments. I am sure you could easily integrate the upload with the mailing.
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Post by aredden »

swiftmailer is very very nice:)

i may implement it once I have things up and stable.

for the record mambo leads on to be more than it actually is :S
Post Reply