upload script+html-(complete newbie)

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

legoman69
Forum Newbie
Posts: 13
Joined: Fri Feb 24, 2006 10:34 am

Post by legoman69 »

The simple version of the script worked fine,
but when I modified it this way:

Code: Select all

<?
//user defined variables
$abpath = "/var/www/vhosts/mysite/subs/images/httpdocs/images"; //Absolute path to where images are uploaded. 
$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);
//begin upload 1

//checks if file exists
if ($_FILES['img1']['name'] == "") {
echo "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)) {
$_FILES['img1']['name'] = $random1 . ".jpeg";
}
if ($img1_type == $cert3) {
$img1_name = $random1 . ".gif";
}
@move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $_FILES['img1']['name']) ;
if (file_exists("$abpath/$img1_name")) {
$log .= "File 1 was uploaded<br>";
}
} else {
$log .= "File 1 is not an image<br>";
}
}
}

?>
I don't get any errors,but the file is not uploaded.
Is the problem in the $img1 again?I changed it to $_FILES['img1']['tmp_name'],but i get errors.
legoman69
Forum Newbie
Posts: 13
Joined: Fri Feb 24, 2006 10:34 am

Post by legoman69 »

I modified a little bit the script,and now it looks like this:

Code: Select all

<?
//user defined variables
$abpath = "/var/www/vhosts/mysite/subs/images/httpdocs/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);
//begin upload 1

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

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

//Checks if file is an image
if (($_FILES['img1']['type'] != $cert1) or ($_FILES['img1']['type'] == $cert2) or ($_FILES['img1']['type'] == $cert3)) {
if (($_FILES['img1']['type'] == $cert1) or ($_FILES['img1']['type'] == $cert2)) {
$_FILES['img1']['name'] = $random1 . ".jpeg";
}
if ($_FILES['img1']['type'] == $cert3) {
$_FILES['img1']['name'] = $random1 . ".gif";
}
@move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $_FILES['img1']['name']) ;
if (file_exists($abpath/ $_FILES['img1']['name'])) {
$log .= "File 1 was uploaded<br>";
}
} else {
$log .= "File 1 is not an image<br>";
}
}
}

?>
But now there are two main problems:

1).jpg and .gif files are uploaded normally,but I don't get the "File 1 was uploaded" msg that is supposed to be the output of $log.

2)Files with any other extensions are uploaded too! But i get a "Warning: Division by zero in /var/www/vhosts/mysite/subs/images/httpdocs/do_upload.php on line 43" error .

----------------
I also changed

Code: Select all

$_FILES['img1']['type']
to

Code: Select all

$_FILES['img1']['tmp_name']
(only in the part which checks the extension)
But the result is the same.

Any hints?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

both of those issues stem from your if using file_exists. i.e. the warning. You've got /, which is a math division operator, trying to create your destination string. Copy it exactly from your call to move_uploaded_file()
LG-2
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 11:21 am

Post by LG-2 »

Mine does work... but not completely. Somehow the files get renamed to some code and its cool that it does but is there a way that it can record the real file name so I can send it to MySQL or something and then to the FTP with the wierd codes it renamed it with.

This is my code:

Code: Select all

<form method=POST action=doupload.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="submit" name="submit" value="Upload">
</form>
<?php
//user defined variables
$abpath = ""; //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
$cert4 = "image/png"; //PNG type
$cert5 = "image/bmp"; //bmp type
$cert6 = "audio/x-mpeg"; //MP3 Sound type
$cert7 = "audio/wav"; //WAV Sound type
$cert8 = "audio/wma"; //Windos Media Audio type
$cert9 = "audio/midi"; //WAV Sound type
$cert10 = "media/wmv"; //Windos Media Video type
$cert11 = "application/x-mplayer2"; //AVI Video type
$cert12 = "application/x-mplayer2"; //MPG type
$cert13 = "application/x-mplayer2"; //MPEG type
$cert14 = "application/x-shockwave-flash"; //SWF Video type

$log = "";
$random1 = rand(1, 99999999);
$random2 = rand(1, 99999999);
$random3 = 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 valid
if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3) or ($img1_type == $cert4) or ($img1_type == $cert5) or ($img1_type == $cert6) or ($img1_type == $cert7) or ($img1_type == $cert8) or ($img1_type == $cert9) or ($img1_type == $cert10) or ($img1_type == $cert11) or ($img1_type == $cert12) or ($img1_type == $cert13) or ($img1_type == $cert14)) {
if (($img1_type == $cert1) or ($img1_type == $cert2)) {
$img1_name = $random1 . ".jpeg";
}
if ($img1_type == $cert3) {
$img1_name = $random1 . ".gif";
}
if ($img1_type == $cert4) {
$img1_name = $random1 . ".png";
}
if ($img1_type == $cert5) {
$img1_name = $random1 . ".bmp";
}
if ($img1_type == $cert6) {
$img1_name = $random1 . ".mp3";
}
if ($img1_type == $cert7) {
$img1_name = $random1 . ".wav";
}
if ($img1_type == $cert8) {
$img1_name = $random1 . ".wma";
}
if ($img1_type == $cert9) {
$img1_name = $random1 . ".midi";
}
if ($img1_type == $cert10) {
$img1_name = $random1 . ".wmv";
}
if ($img1_type == $cert11) {
$img1_name = $random1 . ".avi";
}
if ($img1_type == $cert12) {
$img1_name = $random1 . ".mpg";
}
if ($img1_type == $cert13) {
$img1_name = $random1 . ".mpeg";
}
if ($img1_type == $cert14) {
$img1_name = $random1 . ".swf";
}
@move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $_FILES['img1']['name']) ;
if (file_exists($abpath . '/' . $_FILES['img1']['name'])){
$log .= "$img1_name was uploaded<br>";
}
} else {
$log .= "File 1 is not a valid format.<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";
}
@move_uploaded_file($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 a valid format.<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";
}
@move_uploaded_file($img3, $abpath . '/' . $_FILES['img1']['tmp_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>";
}
}
}
?>
<html>
<head>
<title>Image Report</title>
</head>
<body>
<p>Log:<br>
<?

echo "$log";

?>
Im just working on the first file upload, ignore 2 and 3.

Also, some formats dont want to upload since I guess the profiles are wrong. The ones that upload are: jpg, jpeg, gif, png, wav.
legoman69
Forum Newbie
Posts: 13
Joined: Fri Feb 24, 2006 10:34 am

Post by legoman69 »

I changed the file_exists part to this:

Code: Select all

@move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $_FILES['img1']['name']) ;
if (file_exists($abpath . $_FILES['img1']['name'])) {
$log .= "File 1 was uploaded<br>"; }
and i don't get the "division by zero" error.
But still it's not giving the "File 1 was uploaded",which is supposed to be the output of $log.

The biggest problem is that all files are uploaded,even if they don't have a .jpg or .gif extension,so it seems that the script isn't blocking the files that don't follow the .jpg and .gif rule.
---------------------------------
I also tried this:

Code: Select all

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";
}
But it just uploads everything,without changing the name to a random number.
------------------------
I also changed the $cert part to this(inspired from LG-2s script):

Code: Select all

if (($img1_type == $cert1) or ($img1_type == $cert2)) {
$img1_name = $random1 . ".jpeg";
}
But it's still uploading everything,not changing the filename to a random number,and not showing $log output.
I think i messed up this time..
Any hints?
legoman69
Forum Newbie
Posts: 13
Joined: Fri Feb 24, 2006 10:34 am

Post by legoman69 »

Ok guys,i did some major changes,in order to find out what's the problem in the part of the script which checks the extension of each file.
So,here's the new version:

Code: Select all

<?
//user defined variables
$abpath = "/var/www/vhosts/mysite/subs/images/httpdocs/images"; //Absolute path to where images are uploaded. 
$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);
//begin upload 1


if (($img1_type == $cert1) or ($img1_type == $cert2)) {
move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $_FILES['img1']['name']);
}
else {
$log = "Wrong filetype";
}

?>
This version,uploads any kind of file
So i think the problem is in "$img1_type".
Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Same deal with all the others, $img1_type doesn't exist. Swap it for a $_FILES version.

Not to sound condescending, but have you read Handling File Uploads?
LG-2
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 11:21 am

Post by LG-2 »

I had to put some old stuff in it but I hope this helps. (It works when I tested it).

Code: Select all

<?php
//user defined variables
$fullurl = "http://yoururl.com/files";
$abpath = "yourpath"; //Absolute path to where images are uploaded. No trailing slash
$sizelim = "no"; //Do you want size limit, yes or no
$size = "25000000"; //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
$cert4 = "image/png"; //PNG type
$cert5 = "image/bmp"; //bmp type
$cert6 = "audio/mpeg"; //MP3 Sound type
$cert7 = "audio/wav"; //WAV Sound type
$cert8 = "audio/wma"; //Windos Media Audio type
$cert9 = "audio/midi"; //WAV Sound type
$cert10 = "video/x-msvideo"; //Windos Media Video type
$cert11 = "video/x-msvideo"; //AVI Video type
$cert12 = "video/mpeg"; //MPG type
$cert13 = "video/mpeg"; //MPEG type
$cert14 = "video/quicktime"; // QuickTimeVideo 
$cert14 = "application/x-shockwave-flash"; // SWF

$fileslog = "";
$random1 = rand(1, 999999999);
//begin upload 1

//checks if file exists
if ($img1_name == "") {
$fileslog .= "No seleccionastes un file para subir... Trata otra vez.<br>";
}
if ($_FILES['img1']['name'] != "") {
//checks if file exists
if (file_exists($_FILES['img1']['name'])) {
$fileslog .= "El file ya existe.<br>";
} else {

//checks if files to big
if ($sizelim == "yes") {
if ($_FILES['img1']['size'] > $size) {
$fileslog .= "El file es muy grande.<br>";
}
}

//Checks if file is valid
if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3) or ($img1_type == $cert4) or ($img1_type == $cert5) or ($img1_type == $cert6) or ($img1_type == $cert7) or ($img1_type == $cert8) or ($img1_type == $cert9) or ($img1_type == $cert10) or ($img1_type == $cert11) or ($img1_type == $cert12) or ($img1_type == $cert13) or ($img1_type == $cert14) or ($img1_type == $cert15)) {
if (($img1_type == $cert1) or ($img1_type == $cert2)) {
$img1_name = $random1 . ".jpeg";
}
if ($img1_type == $cert3) {
$img1_name = $random1 . ".gif";
}
if ($img1_type == $cert4) {
$img1_name = $random1 . ".png";
}
if ($img1_type == $cert5) {
$img1_name = $random1 . ".bmp";
}
if ($img1_type == $cert6) {
$img1_name = $random1 . ".mp3";
}
if ($img1_type == $cert7) {
$img1_name = $random1 . ".wav";
}
if ($img1_type == $cert8) {
$img1_name = $random1 . ".wma";
}
if ($img1_type == $cert9) {
$img1_name = $random1 . ".midi";
}
if ($img1_type == $cert10) {
$img1_name = $random1 . ".wmv";
}
if ($img1_type == $cert11) {
$img1_name = $random1 . ".avi";
}
if ($img1_type == $cert12) {
$img1_name = $random1 . ".mpg";
}
if ($img1_type == $cert13) {
$img1_name = $random1 . ".mpeg";
}
if ($img1_type == $cert14) {
$img1_name = $random1 . ".swf";
}
@move_uploaded_file($_FILES['img1']['tmp_name'], $abpath . '/' . $img1_name) ;
if (file_exists($abpath . '/' . $img_name)){
 if (($_FILES['img1']['size']) > 10240) {
$image1size = round($_FILES['img1']['size']/1024000, 2);
$rounded = "MB";
}
 if (($_FILES['img1']['size']) < 1024000) {
$image1size = round($_FILES['img1']['size']/1024, 2);
$rounded = "KB";
}
$fileslog .= "$img1_name ($image1size $rounded) subio al servidor exitosamente.<br>";
}
} else {
$fileslog .= "El file no es valido!<br>";
}
}
}
?>
<style type="text/css" media="screen">@import "/css/upload.css";</style>
<div class="box2"><div class="box">
<font class="h6red">
<?php

echo "$fileslog";

?>
</font></div></div>
Post Reply