PDA

View Full Version : uploading-files?


student101
03-10-2008, 10:07 AM
This tut:
http://www.dreamweaverclub.com/vtm/uploading-files.php
Trying the type parts, but no luck?

session_start();
if(isset($_POST['uploader'])){
if ($_FILES['uploadedfile'] == '') {
$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else {
if ("type" != "image/gif") {
$error = '<font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Error in upload type</font>';
} else {

echo "<pre>";
print_r($_FILES);
echo "</pre>";

$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);
}
}
}

Any ideas?

davidj
03-10-2008, 10:14 AM
what are the type parts

this is a vague question

student101
03-10-2008, 10:28 AM
Ok,
So how do I add this file type pattern instead?

$pattern="\.(jpg|jpeg|png|gif|bmp)$";

Only allow these types of files?

davidj
03-10-2008, 01:40 PM
ok

i understand

i think it would be a good idea to use a switch to id your mime types



if(isset($_POST['uploader'])){

if ($_FILES['uploadedfile'] == '') {

$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else {

if ("type" != "image/gif") {

$error = '<font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Error in upload type</font>';

}else{

echo "<pre>";
print_r($_FILES);
echo "</pre>";

$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];

move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);

}
}
}


ok

this is the bit that determins the mime type of the file you have uploaded


if ("type" != "image/gif")



ok we are going to replace the condition with a boolean true/false

above the IF you add a switch..

switch($_FILES["uploadedfile"]["type"]){

case 'image/gif': $type = true; break;
case 'image/jpg': $type = true; break;
case 'image/png': $type = true; break;

/// add the rest yourself

}

/// the IF below replaces the IF above ///
if ($type == true) { /// etc


mime types...
http://www.webmaster-toolkit.com/mime-types.shtml

you can use the above or use an array to store the mime types

if you work out a method which uses an array ill award you with a DWC medal

it has to work though

student101
03-10-2008, 01:42 PM
what are the type parts
this is a vague question

Here are the parts of the code:
I have got no clue on how or what goes where?

The upload form with the HTML code: :-D

echo "<pre>";
print_r($_FILES);
echo "</pre>";

$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" />
<input name="uploader" type="submit" id="uploader" value="Upload File" />
<br />
<?php //echo $error; would like to use it but no idea how?>
</form>
</body>
</html>
The if then else parts: //which goes where? :?

if ((($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg"))
&& ($_FILES["uploadedfile"]["size"] < 100000)) //set file size here
{
if ($_FILES["uploadedfile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br />";
}else{
echo $_FILES["uploadedfile"]["name"] . " already exists. ";
}else{
//upload the file! (I think?)
}
I hope this makes sense, cause to me it don't. :mrgreen:
Cheers

student101
03-10-2008, 01:45 PM
Sh1t you posted just just before me,
hectic!

davidj
03-10-2008, 01:47 PM
if you dont understand it then you are not learning

you need to understand every aspect as i wont finish any code so people can copy and paste. I let you complete my examples as its my way to teach. I never write solutions for anyone. If you are copying (parrot fashion) my tutorials then you wont benefit from them

student101
03-10-2008, 02:02 PM
if you dont understand it then you are not learning
you need to understand every aspect as i wont finish any code so people can copy and paste. I let you complete my examples as its my way to teach. I never write solutions for anyone. If you are copying (parrot fashion) my tutorials then you wont benefit from them

You've lost me?

Your video has file downloads, that's what I added along with the "if then else" code.
The problem I have is knowing where to place those if then else parts?

Cheers

student101
03-10-2008, 02:06 PM
By the way,
those mime types is what I used for my forcing a file download, instead of opening the file and waisting bandwidth.
Funny, I never did save that bookmark, thanks for the link.

davidj
03-10-2008, 02:11 PM
its good to collect some good links

tip: keep the links on a USB pen drive so you can access them anywhere.

ok

did my example become clear?

student101
03-10-2008, 02:21 PM
Sort of,
I have never understood the if then else or switch in PHP.

Does this
if ($type == true) {
replace this?

if ("type" != "image/gif") {

That part also doesn't make sense to me.

The first one says if it's true meaning "+"positive then do something...
The second one says if it's false meaning "-"negative then do something...
So which one is it?

Does that make sense?

student101
03-10-2008, 02:23 PM
This the entire page now, is it right?
I must add php comments where needed for future.

<?php
if(isset($_POST['uploader'])){
if ($_FILES['uploadedfile'] == '') {
$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else {
switch($_FILES["uploadedfile"]["type"]){
case 'image/gif': $type = true; break;
case 'image/jpg': $type = true; break;
case 'image/jpeg': $type = true; break;
case 'image/png': $type = true; break;
/// add the rest yourself
}
/// the IF below replaces the IF above ///
if ($type == true) { /// etc
$error = '<font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Error in upload type</font>';
}else{
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" />
<input name="uploader" type="submit" id="uploader" value="Upload File" />
<br />
<?php echo $error; ?>
</form>
</body>
</html>

davidj
03-10-2008, 02:57 PM
I have never understood the if then else or switch in PHP.


ok

lesson on conditions


if(condition is met here){

// run code in here

}



a switch...


$var = "string3";

// match contents in $var
switch($var){

case 'string1': // does the contents of the var match the case
echo "the variable contains the string ".$var; // yes it does so do stuff
break; // break out of the switch to stop further switch process if true or move to the next case if false

case 'string2': // does the contents of the var match the case
echo "the variable contains the string ".$var; // yes it does so do stuff
break; // break out of the switch to stop further switch process if true or move to the next case if false

case 'string3': // does the contents of the var match the case
echo "the variable contains the string ".$var; // yes it does so do stuff
break; // break out of the switch to stop further switch process if true or move to the next case if false
}

davidj
03-10-2008, 03:05 PM
your logic is backward


if ($type == true) { /// etc
$error = '<font color="#FF0000 .....


you are saying that if your image type id is true (the type being identifyed by your switch) then error.

look again at your condition and reverse it so it throws the error when it cant identify the type or the $type != true or ! $type

i gave you a clue there

student101
03-10-2008, 03:06 PM
Thank you, it makes more sense now - except on little thing.
In the if then else part...


if(condition is met here){
// Run this code if true? "not false"
} else {
// Rather run this code cause the condition is false? "not true"
}

Will it always be "true" or "false" or could it be "false" then "true"?

I hope that makes sense. :?

davidj
03-10-2008, 03:10 PM
the test will only be true or false at any one time unless you change it

it cant be both

davidj
03-10-2008, 03:12 PM
oh sorry

i just understood your post

davidj
03-10-2008, 03:14 PM
the opposite is like this



$var = false;

if($var != true){
// Run this code if not true? or false
} else {
// Rather run this code cause the condition is true?
}

student101
03-10-2008, 03:19 PM
That's it!
Which one is true and which one is false?

Where do you place the true statement and where do you place the false part?

Is the true part always first?

Like this:

if $name == ""; {//blank then
echo "Your name is $name";
}else{
echo "Sorry your name is blank";
}

student101
03-10-2008, 03:21 PM
I was replying to other post, you are too fast for me.

student101
03-10-2008, 03:23 PM
the opposite is like this

$var = false;
if($var != true){
// Run this code if not true? or false
} else {
// Rather run this code cause the condition is true?
}


How do you know the variable before hand?
That part can only be found after or during the upload.

So if I understand it now:

//$var = false; //Know clue yet
if($var != true){
// Run this code if false?
} else {
// Rather run this code cause the condition is true?
}

davidj
03-10-2008, 03:25 PM
using this example...


$name = "davidj";


example1...

if($name){

// this will run

}else{

// this wont run

}



example2...

if(! $name){

// this wont run

}else{

// this will run

}


example3...

if($name !=""){

// this will run

}else{

// this wont run

}


example4...

if($name ==""){

// this wont run

}else{

// this will run

}


example5...

if($name == "davidj"){

// this will run

}else{

// this wont run

}

student101
03-10-2008, 03:30 PM
Ok think I got it.
There are parts that don't always work though?

<?php
if(isset($_POST['uploader'])){
if ($_FILES['uploadedfile'] == '') { //this line or part doesn't always work?
$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else {
switch($_FILES["uploadedfile"]["type"]){
case 'image/gif': $type = true; break;
case 'image/jpg': $type = true; break;
case 'image/jpeg': $type = true; break;
case 'image/png': $type = true; break;
}
if (!$type) { /// this part also doesn't always work?
$error = '<font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Error in upload type</font>';
}else{
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);
}
}
}
?>

student101
03-10-2008, 03:39 PM
This part is broken?
It always throws the second error.

if(isset($_POST['uploader'])){
if ($_FILES['uploadedfile'] == '') { //this line or part doesn't always work?
$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else{

davidj
03-10-2008, 03:43 PM
if ($_FILES['uploadedfile'] == '') { //this line or part doesn't always work?

thats because its a super global variable and its a multi dimensional array

you are missing a key

it should be ...


if ($_FILES['uploadedfile']['name']=="")


with uploadedfile being the field name and name being one of a set of pre defined keys which give you information about the file you uploaded

to view what other keys are available to you you can use a

print_r($_FILES);

student101
03-10-2008, 03:48 PM
Thank you.
It now makes actual sense.

Some times I get so lost even though it's right, I confuse myself! (and others)

student101
03-10-2008, 03:49 PM
You could turn this into a sticky.

davidj
03-10-2008, 03:50 PM
i have 200 posts just like this one

i will be going over this again next week. Its just the way it is

student101
03-10-2008, 03:59 PM
Here is the complete file upload code / tut with basic restrictions.

<?php
if (isset($_POST['uploader'])){
if ($_FILES['uploadedfile']['name']==""){
$error = '<strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Nothing submitted?</font></strong><br>';
} else {
switch($_FILES["uploadedfile"]["type"]){
case 'image/gif': $type = true; break;
case 'image/jpg': $type = true; break;
case 'image/jpeg': $type = true; break;
case 'image/png': $type = true; break;
case 'image/bmp': $type = true; break;
case 'image/x-windows-bmp': $type = true; break;
}
if (!$type) {
$error = '<font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">Error in upload type</font>';
}else{
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$target_path = "C:/Documents and Settings/Jay/My Documents/sites/jtechsystems-php/upload/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> take away the html comments if needed-->
Choose a file to upload: <input name="uploadedfile" type="file" />
<input name="uploader" type="submit" id="uploader" value="Upload File" />
<br />
<?php echo $error; ?>
</form>
<?php
//print_r($_FILES); //to get attributes of the files
?>
</body>
</html>


Thank you.

Cheers

davidj
03-10-2008, 04:02 PM
you can remove...


echo "<pre>";
print_r($_FILES);
echo "</pre>";


as this is just for debugging

student101
03-10-2008, 04:11 PM
Cool.
Thank you for helping figure out this PHP stuff.
It's the only thing that used to bother me.

Cheers

davidj
03-10-2008, 04:13 PM
no problem