Forum Moderators: coopster

Message Too Old, No Replies

Parse error help, please

         

Captain Morgan

8:29 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi All,

I am new to php and I am trying to modify this script I found online and I am receiving this error

Parse error: parse error, unexpected ';', expecting ')' on line 9

Here is the complete script

<?php
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Mmaximum file size. You may increase or decrease.
$MAX_SIZE = 2000000;

//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('image/jpeg','image/jpg','image/gif','image/png','application/msword','application/x-shockwave-flash';

//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.jpg','.png','.gif','.swf';

//Allow file delete? no, if only allow upload only
$DELETABLE = true;

//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
* Setup variables
************************************************************/
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = Array )
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);
$upload_url = $url_dir."/files/";
$message ="";

/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed.");
}

/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);

if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url[$file_ext]);

print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";

/************************************************************
* List Files
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) &&!is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
if ($DELETABLE)
$filelist .= " <a href='?del=$upload_dir$file' title='delete'>x</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}

?>

<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>

<br><b>My Files</b>
<hr width=70%>
<?=$filelist?>
<hr width=70%>
<small><sup>Developed By
<a style="text-decoration:none" href="http://tech.citypost.ca">CityPost.ca</a>
</sup></small>
</center>

THANKS for any help on this!
Joe

sned

8:38 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi Joe, welcome to WebmasterWorld ...

It looks like you're missing a ')' on the end of these two lines:

$FILE_MIMES = array('image/jpeg','image/jpg','image/gif','image/png','application/msword','application/x-shockwave-flash';

$FILE_EXTS = array('.jpg','.png','.gif','.swf';

Should be :

$FILE_MIMES = array('image/jpeg','image/jpg','image/gif','image/png','application/msword','application/x-shockwave-flash');
$FILE_EXTS = array('.jpg','.png','.gif','.swf');

-sned

<added>
Also, later on down I see this:

$upload_dir = Array )

If it's not a typo, it should be: $upload_dir = Array (
</added>

Captain Morgan

9:24 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi, Sned!

THANKS for the help, I am getting closer for sure!

I am now getting these errors

Warning: opendir(Array): failed to open dir: No such file or directory in /home/dynodo2/public_html/games1/upload.php on line 91

Warning: readdir(): supplied argument is not a valid Directory resource in /home/dynodo2/public_html/games1/upload.php on line 93

sned

9:34 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



hmm, I'm not sure what the code is trying to accomplish in these lines:

$upload_dir = Array (
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);
$upload_url = $url_dir."/files/";

ahhh ... I think I get it ... what happens if you switch the variable names around ...:

$upload_url = Array (
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);
$upload_dir = $url_dir."/files/";

Captain Morgan

9:45 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi, Sned!

Well, I am trying to get the script to upload jpgs, gifs to the games1/images directory and swf to games1/files directory

Switch the variable names around?

Thanks,
Joe

sned

9:54 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



I'm thinking that switching the variable names around might help for a couple reasons:

Here's the variables as they are now:


$upload_dir = Array (
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);


$upload_url = $url_dir."/files/";

Here's the function declaration:


function do_upload($upload_dir, $upload_url) { ...

But, when the function is called:

$message = do_upload($upload_dir, $upload_url[$file_ext]);
.. things are switched around, so in the first place the variable names were mixed up.

So instead of:
$upload_dir = Array (
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);
$upload_url = $url_dir."/files/";

I would try this:
$upload_url = Array (
".gif" => "images/",
".jpg" => "images/",
".swf" => "files/"
);
$upload_dir = $url_dir."/files/";

They're very confusing variable names.

Captain Morgan

10:05 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Ok, I see what mean.

I am still getting that same error :(

I appreciate your time and help!
Joe

sned

10:09 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Ok, what happens if you do this: (what prints out)

echo "The upload dir is : $upload_dir";
$handle=opendir($upload_dir);

I think thats around line 90 - 91 or so.

Captain Morgan

10:23 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi, Sned!

This is what I get

The upload dir is : http://example.com/games1/files/
Warning: opendir(http://example.com/games1/files/): failed to open dir: not implemented in /home/example/public_html/games1/upload.php on line 91

Warning: readdir(): supplied argument is not a valid Directory resource in /home/example/public_html/games1/upload.php on line 93

[edited by: jatar_k at 10:53 pm (utc) on July 15, 2005]
[edit reason] no personal urls thanks [/edit]

sned

10:34 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hmm, I'm wondering if it's the http:// stuff etc thats messing it up....

What if you changed this:
$upload_dir = $url_dir."/files/";

To read like this:
$upload_dir = "/home/dynodo2/public_html/games1/files/";

<edit>
Oops, added '/' to the end
</edit>

Captain Morgan

10:57 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



WOO HOO...that stopped the error.

But, it is uploading everthing (images and swf) to the games1/files directory. It is supposed to upload images to games1/images directory and swf to games1/files directory

Thanks,
Joe

sned

11:01 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Right, because there never was any code to switch the directories depending on file type, I guess it got left out or forgotten in the process somehow.

I'll see if I can figure out a way to do it here quickly. ...

sned

11:14 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Ok, here's a way to do it, however, it's not gonna display a list of files like I think you would like it to do.

First, make your $upload_url array look like this:
$upload_url = Array (
".gif" => "images/",
".jpg" => "images/",
".png" => "images/",
".swf" => "files/"
);

Then change your $upload_dir from:
$upload_dir = "/home/dynodo2/public_html/games1/files/";

to this:

$upload_dir = "/home/dynodo2/public_html/games1/";

Then, in the function do_upload:

change this:
$file_path = $upload_dir.$file_name;

to this:
$file_path = $upload_dir.$upload_url.$file_name;

So this will load pngs, gifs, and jpgs to games1/images/ and swf files to /games1/files/

However, it's just going to display a list of what's in the games1 directory right now .. you can display the other directorys, but unfortunately I don't have time to show you how to do that right now ....

-sned

Captain Morgan

11:20 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi, Sned!

Well, I really appreciate all the time and help you gave me!

THANKS,
Joe