Page 1 of 2

upload script+html-(complete newbie)

Posted: Fri Feb 24, 2006 1:00 pm
by legoman69
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi,
I've found a script that allows users to upload files to a web folder,but i can't get it work!it's driving me crazy(propably because i don't know anything about php),so if someone can give me a clue about what i'm doing wrong.

here's the html form i use:

Code: Select all

<td>
<form method=POST action=do_upload.php enctype=multipart/form-data>
<p>Files to upload:<br>
<input type=file name=img1 size=30><br>
<input type=file name=img2 size=30><br>
<input type=file name=img3 size=30><br>
<input type=file name=img4 size=30><br>
<input type=file name=img5 size=30><br>
<input type="submit" name="submit" value="Upload"> 
</form>
</td>
and here's the php part:

Code: Select all

<?
//user defined variables
$abpath = "/images"; //Absolute path to where images are uploaded. No trailing slash
$sizelim = "no"; //Do you want size limit, yes or no
$size = "2500000"; //What do you want size limited to be if there is one

//all image types to upload
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type

$log = "";
$random1 = rand(1, 99999999);
$random2 = rand(1, 99999999);
$random3 = rand(1, 99999999);
$random4 = rand(1, 99999999);
$random5 = rand(1, 99999999);


//begin upload 1

//checks if file exists
if ($img1_name == "") {
$log .= "No file selected for upload 1<br>";
}
if ($img1_name != "") {
//checks if file exists
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img1_size > $size) {
$log .= "File 1 was too big<br>";
}
}

//Checks if file is an image
if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3)) {
if (($img1_type == $cert1) or ($img1_type == $cert2)) {
$img1_name = $random1 . ".jpeg";
}
if ($img1_type == $cert3) {
$img1_name = $random1 . ".gif";
}
@copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server<br>";
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 was uploaded<br>";
}
} else {
$log .= "File 1 is not an image<br>";
}
}
}
//checks if file exists
if ($img2_name == "") {
$log .= "No file selected for upload 2<br>";
}
if ($img2_name != "") {
//checks if file exists
if (file_exists("$abpath/$img2_name")) {
$log .= "File 2 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img2_size > $size) {
$log .= "File 2 was too big]<br>";
}
}

//Checks if file is an image
if (($img2_type == $cert1) or ($img2_type == $cert2) or ($img2_type == $cert3)) {
if (($img2_type == $cert1) or ($img2_type == $cert2)) {
$img2_name = $random2 . ".jpeg";
}
if ($img2_type == $cert3) {
$img2_name = $random2 . ".gif";
}
@copy($img2, "$abpath/$img2_name") or $log .= "Couldn't copy image 2 to server<br>";
if (file_exists("$abpath/$img2_name")) {
$log .= "File 2 was uploaded<br>";
}
} else {
$log .= "File 2 is not an image<br>";
}
}
}
if ($img3_name == "") {
$log .= "No file selected for upload 3<br>";
}
if ($img3_name != "") {
//checks if file exists
if (file_exists("$abpath/$img3_name")) {
$log .= "File 3 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img3_size > $size) {
$log .= "File 3 was too big<br>";
}
}

//Checks if file is an image
if (($img3_type == $cert1) or ($img3_type == $cert2) or ($img3_type == $cert3)) {
if (($img3_type == $cert1) or ($img3_type == $cert2)) {
$img3_name = $random3 . ".jpeg";
}
if ($img3_type == $cert3) {
$img3_name = $random3 . ".gif";
}
@copy($img3, "$abpath/$img3_name") or $log .= "Couldn't copy image 3 to server<br>";
if (file_exists("$abpath/$img3_name")) {
$log .= "File 3 was uploaded<br>";
}
} else {
$log .= "File 3 is not an image<br>";
}
}
}
if ($img4_name == "") {
$log .= "No file selected for upload 4<br>";
}
if ($img4_name != "") {

//checks if file exists
if (file_exists("$abpath/$img4_name")) {
$log .= "File 4 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img4_size > $size) {
$log .= "File 4 was too big<br>";
}
}

//Checks if file is an image
if (($img4_type == $cert1) or ($img4_type == $cert2) or ($img4_type == $cert3)) {
if (($img4_type == $cert1) or ($img4_type == $cert2)) {
$img4_name = $random4 . ".jpeg";
}
if ($img4_type == $cert3) {
$img4_name = $random4 . ".gif";
}
@copy($img4, "$abpath/$img4_name") or $log .= "Couldn't copy image 4 to server<br>";
if (file_exists("$abpath/$img4_name")) {
$log .= "File 4 was uploaded<br>";
}
} else {
$log .= "File 4 is not an image<br>";
}
}
}

if ($img5_name == "") {
$log .= "No file selected for upload 5<br>";
}
if ($img5_name != "") {
//checks if file exists
if (file_exists("$abpath/$img5_name")) {
$log .= "File 5 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img5_size > $size) {
$log .= "File 5 was too big<br>";
}
}

//Checks if file is an image
if (($img5_type == $cert1) or ($img5_type == $cert2) or ($img5_type == $cert3)) {
if (($img5_type == $cert1) or ($img5_type == $cert2)) {
$img5_name = $random5 . ".jpeg";
}
if ($img1_type == $cert3) {
$img5_name = $random5 . ".gif";
}
@copy($img5, "$abpath/$img5_name") or $log .= "Couldn't copy image 5 to server<br>";
if (file_exists("$abpath/$img5_name")) {
$log .= "File 5 was uploaded<br>";
}

} else {
$log .= "File 5 is not an image<br>";
}
}
}
?>
<html>
<head>
<title>Image Report</title>
</head>
<body>
<p>Log:<br>
<?

echo "$log";

?>
</p>
<body>
<html>
(the comments helped a lot)
so,when i press "upload" from the form,it says:

Code: Select all

No file selected for upload 1
           >> >>     >>      >>    >>   2
(etc..)
i've played around with permisions and stuff,but it's not working..
and actually,i waas thinking if i could simplify someway the whole script,cause i don't really need the multiple upload option.
any suggestions?
thx

ps.sorry for the huge post.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Feb 24, 2006 1:46 pm
by feyd
Run the following in a new file and tell us the results please.

Code: Select all

<?php

$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$eol = (isset($_SERVER['HTTP_HOST']) ? "<br />\n" : "\n");

$ec = array(
	'E_STRICT' => 2048,
	'E_ALL' => 2047,
	'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512,
	'E_USER_ERROR' => 256,
	'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64,
	'E_CORE_WARNING' => 32,
	'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8,
	'E_PARSE' => 4,
	'E_WARNING' => 2,
	'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
$er = $er . ' (' . implode(' | ', $e) . ')';

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Display Errors: ' . $de . $eol;

?>

Posted: Fri Feb 24, 2006 2:22 pm
by legoman69
thx for the fast reply.
I hope by "results",you mean this:

Code: Select all

PHP Version: 4.3.2
PHP OS: Linux
Error Reporting: 2039 (E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_WARNING | E_ERROR)
Register Globals: Off
Display Errors: On
how bad is it? :)

Posted: Fri Feb 24, 2006 2:36 pm
by feyd
Your code was written with the assumption Register Globals is turned on. As you can see, the output of that little test script says it is not on. Your script should be rewritten. $img1_name would change to $_FILES['img1']['name'], $img2_size would change to $_FILES['img2']['size'] and so forth.

Posted: Fri Feb 24, 2006 3:04 pm
by legoman69
ok,i understand the problem.

So,i modified the part which refers to file 1,this way:

Code: Select all

//begin upload 1

//checks if file exists
if ($_FILES['img1']['name'] == "") {
$log .= "No file selected for upload 1<br>";
}
if ($_FILES['img1']['name'] != "") {
//checks if file exists
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 already existed<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($img1_size > $size) {
$log .= "File 1 was too big<br>";
}
}

//Checks if file is an image
if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3)) {
if (($img1_type == $cert1) or ($img1_type == $cert2)) {
$_FILES['img1']['name'] = $random1 . ".jpeg";
}
if ($img1_type == $cert3) {
$_FILES['img1']['name'] = $random1 . ".gif";
}
@copy($img1, "$abpath/$_FILES['img1']['name']") or $log .= "Couldn't copy image 1 to server<br>";
if (file_exists("$abpath/$_FILES['img1']['name']")) {
$log .= "File 1 was uploaded<br>";
}
} else {
$log .= "File 1 is not an image<br>";
}
}
}
But now,i get this:

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /var/www/vhosts/mysite/subs/images/httpdocs/do_upload.php on line 47
So,there seems to be some problem with this line:

Code: Select all

@copy($img1, "$abpath/$_FILES['img1']['name']") or $log .= "Couldn't copy image 1 to server<br>";
Also,something else that i noticed is that if I leave line 47 as it was:

Code: Select all

@copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server<br>";
and line 28 as it was:

Code: Select all

if (file_exists("$abpath/$img1_name")) {
I get this:

Code: Select all

Warning: file_exists(): open_basedir restriction in effect. File(/images/) is not within the allowed path(s): (/var/www/vhosts/mysite/subs/images/httpdocs:/tmp) in /var/www/vhosts/mysite/subs/images/httpdocs/do_upload.php on line 28
and the log output is this:

Code: Select all

Log:
File 1 is not an image
No file selected for upload 2
No file selected for upload 3
No file selected for upload 4
No file selected for upload 5
I don't know if it helps,but I just HAD to say it :D

Posted: Fri Feb 24, 2006 3:28 pm
by feyd
copy() should be move_uploaded_file()

Code: Select all

"$abpath/$_FILES['img1']['name']"
// to
$abpath . '/' . $_FILES['img1']['name']

Posted: Fri Feb 24, 2006 3:39 pm
by legoman69
I modified this part:

Code: Select all

move_uploaded_file($img1, "$abpath/$_FILES['img1']['name']") // to
$abpath . '/images' . $_FILES['img1']['name'] or $log .= "Couldn't copy image 1 to server<br>";
if (file_exists("$abpath/$_FILES['img1']['name']")) {
$log .= "File 1 was uploaded<br>";
also i modified every "$img1_name" to "$_FILES['img1']['name']"

but now i get this:

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /var/www/vhosts/mysite/subs/images/httpdocs/do_upload.php on line 28
So,there seems to be a problem with this line:

Code: Select all

if (file_exists("$abpath/$_FILES['img1']['name']")) {
but I'm not sure if i used correctly the "move_uploaded_file"....
?

Posted: Fri Feb 24, 2006 4:09 pm
by feyd
read the second part of my last post and this.

Posted: Fri Feb 24, 2006 5:40 pm
by legoman69
i've read your link,but no luck :(
I've got this far:

Code: Select all

move_uploaded_file("$abpath/$_FILES['img1']['name']", 
$abpath . '/var/www/vhosts/ultratech.gr/subdomains/images/httpdocs' . $_FILES['img1']['name'] ) ;
but i still get this:

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /var/www/vhosts/mysite/subs/images/httpdocs/do_upload.php on line 44
i give up...
any other hints?

thx

Posted: Fri Feb 24, 2006 6:21 pm
by feyd
every place where you're using $_FILES inside an existing string should be moved out of said string, just like the example I gave in the second part of my previous post.

Posted: Sat Feb 25, 2006 11:10 am
by legoman69
I modified the script this way,to meet my needs:

Code: Select all

<?
//user defined variables
$abpath = "/var/www/vhosts/mysite/subs/images/httpdocs/images"; //Absolute path to upload folder
$sizelim = "no"; //Do you want size limit, yes or no
$size = "2500000"; //What do you want size limited to be if there is one

//all image types to upload
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2


$log = "";

$random1 = rand(1, 99999999);
//begin upload 1

$img1_name = $random1 . ".jpeg";

@move_uploaded_file($img1, $abpath . '/' . $_FILES['img1']['name']) ;
?>
Now I don't get any errors,but the file is not copied,to the directory I defined,at all.
Any idea what did I do wrong this time? :)

Posted: Sat Feb 25, 2006 11:12 am
by feyd
where's $img1 coming from?

Posted: Sat Feb 25, 2006 11:29 am
by legoman69
Good question :D
I think from here:

Code: Select all

<form method=POST action=do_upload.php enctype=multipart/form-data>
<p>Files to upload:<br>
<input type=file name=img1 size=30><br>
<input type="submit" name="submit" value="Upload">

Posted: Sat Feb 25, 2006 11:33 am
by feyd
change it to $_FILES['img1']['tmp_name']

Posted: Sat Feb 25, 2006 11:39 am
by legoman69
ohh yeaahh,now we're talking!
It's working,finally! :D

"In the begining I just wanted to use an upload script..Now i think it would be useful to learn some php." (c) Another satisfied customer

thanks a lot feyd :wink: