Jump to content

Archived

This topic is now archived and is closed to further replies.

jpratt

file upload problem

Recommended Posts

I have the following script i am working with to upload an image. as far as i can tell the code works fine. When i go to display the page it wont come up and wont give an error, just a blank white page:

[code]
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$me = $_SERVER['PHP_SELF'];
?>
<form name="form1" method="post" action="<?php echo $me;?>" enctype="multipart/form-data">
              <table width="500" border="0" cellspacing="3" cellpadding="0">
                <tr>
                  <td width="256" height="30" align="right" valign="middle"><p align="right"><span class="style1">Comment for Photo</span></p></td>
                  <td width="344" align="left"><input name="textfield" type="text" size="40" /></td>
                </tr>
                <tr>
                  <td height="30" align="right" class="style1">Website Link </td>
                  <td align="left"><input type="text" name="textfield2" />
                    <span class="style1">ie. www.google.com</span> </td>
                </tr>
                <tr>
                  <td height="30" align="right"><span class="style1">Photo </span></td>
                  <td align="left"><input type="file" name="imagefile" /></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td align="left"><input name="Submit" type="submit" value="Submit" /></td>
                </tr>
              </table>
              </form>
  <?php
  }
  else{
  if(isset($Submit)){
  $file=$_FILES['imagefile']['name'];
  $filetype=substr($file,-4);
  if($filetype=="jpeg"){
    copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name'])
    or die("Could not copy");
    echo"<br>Upload Complete";
    echo"<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
    echo"<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
    echo"<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
  }elseif($filetype==".jpg"){
    copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name'])
    or die("Could not copy");
    echo"<br>Upload Complete";
    echo"<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
    echo"<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
    echo"<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
  }elseif($filetype==".png"){
    copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name'])
    or die("Could not copy");
    echo"<br>Upload Complete";
    echo"<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
    echo"<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
    echo"<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
  }elseif($filetype==".gif"){
    copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name'])
    or die("Could not copy");
    echo"<br>Upload Complete";
    echo"<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
    echo"<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
    echo"<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
  }else{
    echo"<br><span class="subtitle">Upload Error</span>";
    echo"<br>Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
  }
}
}
?>[/code]

Any ideas?? Please Help!

Share this post


Link to post
Share on other sites
I am not a php expert, but, you have two php opening tags...
Try closing the first one.

Share this post


Link to post
Share on other sites
You have a syntax error on this line:

[code]echo"<br><span class="subtitle">Upload Error</span>"; [/code]

Here is a cleaner version of your code:

[code]<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$me = $_SERVER['PHP_SELF'];
echo '
<form name="form1" method="post" action="' . $me . '" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="3" cellpadding="0">
<tr>
<td width="256" height="30" align="right" valign="middle"><p align="right"><span class="style1">Comment for Photo</span></p></td>
<td width="344" align="left"><input name="textfield" type="text" size="40" /></td>
</tr>
<tr>
<td height="30" align="right" class="style1">Website Link </td>
<td align="left">
<input type="text" name="textfield2" /> <span class="style1">ie. www.google.com</span>
</td>
</tr>
<tr>
<td height="30" align="right"><span class="style1">Photo </span></td>
<td align="left"><input type="file" name="imagefile" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="left"><input name="Submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>';
} else {
if (isset($Submit)) {
$file = $_FILES['imagefile']['name'];
$filetype = substr($file,-4);
if ($filetype=="jpeg") {
copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else if ($filetype==".jpg") {
copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else if ($filetype==".png") {
copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else if ($filetype==".gif") {
copy($_FILES['imagefile']['tmp_name'],"UploadImg/".$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else {
echo "<br><span class=\"subtitle\">Upload Error</span>";
echo "<br>Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
}
}
}
?>[/code]

Share this post


Link to post
Share on other sites
thanks for the clean up. but it still does not function. I have checked my php.ini and verified it allowed uploads. Somthing is still wrong.

Share this post


Link to post
Share on other sites
Cant get this thing to work, does anyone have a better way of doing a file upload.

Share this post


Link to post
Share on other sites
I have run through my php.ini file, checked with my host to make sure this is allowed, and still cont get this thing to work. I know the post is happening, but nothing is being displayed, no confirmation or denial of upload. Anymore suggestions other than how to clean up my code?

Share this post


Link to post
Share on other sites
Well, one issue may be that when you submit the form it's taking you to copy.php instead of staying at upload.php to continue the script.

Share this post


Link to post
Share on other sites
please look at the php.net website and look at the correct upload method as from what i can see your setting to meny varables that can be set as one in an array ok.

good luck.

Share this post


Link to post
Share on other sites
sorry, i have been testing locally with the code above. The accual page is now at www.myplasmaart.com/upload.php. I am not using anything different that above, except i fixed the echo statement at the end. I am not even getting the "Upload Error" in my else if statement

Share this post


Link to post
Share on other sites
I tried to upload a .jpg and got this:

[code]Possible file upload attack!
Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => badboys2.sized.gif
            [type] =>
            [tmp_name] =>
            [error] => 2
            [size] => 0
        )

)[/code]

Share this post


Link to post
Share on other sites
this is the correct code to use to test a user file type ok.
$type=($_FILES['userfile']['type']=="video/mpeg");

Share this post


Link to post
Share on other sites
Yes, that and I believe you're using the wrong function to get the file types.  You sould strip the image string up to the . (dot) and take that extension.  But I'm not too good with file uploads :)

Share this post


Link to post
Share on other sites
take a quick look at this as you can see from my code you also need to add valadation to it ok.

but look and also go and study from the php.net website remember copy and past is time consumming when you get another project bes to learn properly ok.

You could code the way your coding but if you want to better your self your have to understand arrays and also read sorry i no it's boring but when your earning nice fat pocket load of money that wont matter then.


good luck.

example only look at the code properly .
[code]
<?php

// file upload information

$uploaddir ="members_uploads/";

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

//set a date sent

$date_sent=date("d-m-y");

// set a mime valadating

$blah = getimagesize($userfile);
$type = $blah['mime'];
$width = $blah[0];
$size = $blah[2]=$_FILES['userfile']['size'];



// valadate siz and type

if($size <= 50000) {

if($type) {

// if file correct let file throw and into the folder

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) {

$userfile_name=addslashes($userfile_name);

$query="insert into members_picture_uploads values('$id','$name','$date_sent','$userfile_name')";

$result=mysql_query($query);


// say thank you if the user has a valid file.

echo "file uploaded thank you<br>";
exit;
}

// echo link for valadating pic
}else{
echo "Wrong file type .jpg or .gif thank you <br><br> <a href ='members_upload_picture_form.php'>Pleae try agin</A>";
}

//echo link for valadating file size
}else{
echo "Wrong file size 50000 bytes only<br><br> <a href ='members_upload_picture_form.php'>Pleae try agin</A>";
}

?>
[/code]

Share this post


Link to post
Share on other sites
ok i have tried your code and it still will not upload. I have read the entire section on uploads from php.net. When i use your code it always tells me it is not the right file type. I have tried uploading .jpg, .gif. I left out the database code you had in there so it looks like this:

[code]
<?php

// file upload information

$uploaddir ="uploadimg/";

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);


// set a mime valadating

$blah = getimagesize($userfile);
$type = $blah['mime'];
$width = $blah[0];
$size = $blah[2]=$_FILES['userfile']['size'];



if($size <= 2000000) {

//if($type) {

// if file correct let file throw and into the folder

if(move_uploaded_file($_FILES['userfile']['name'],$uploadfile)) {


// say thank you if the user has a valid file.

echo "file uploaded thank you<br>";
exit;
}

// echo link for valadating pic
}else{
echo "Wrong file type .jpg or .gif thank you <br><br> <a href ='upload.php'>Pleae try again</a>";
}

echo link for valadating file size
}else{
echo "Wrong file size 2000000 bytes only<br><br> <a href ='upload.php'>Pleae try again</a>";
}

?>
[/code]

This is driving me crazy!

Share this post


Link to post
Share on other sites
ok i went back to my original plan. the code i had before was so close. I do understand arrays, but 3 if statements does not seenm to exsesive. Anyway i am alomost there. with this code I can upload the file if i leave the directory as '', this places the file in my root directory of course. i do have a folder called uploadimg i would like to place them in. when i set my directory in the code to 'uploadimg/' it says it uploaded successfuly but it accually does not do anything. here is what i am using now:

[code]
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$me = $_SERVER['PHP_SELF'];
echo '
<form name="form1" method="post" action="' . $me . '" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="3" cellpadding="0">
<tr>
<td width="256" height="30" align="right" valign="middle"><p align="right"><span class="style1">Comment for Photo</span></p></td>
<td width="344" align="left"><input name="textfield" type="text" size="40" /></td>
</tr>
<tr>
<td height="30" align="right" class="style1">Website Link </td>
<td align="left">
<input type="text" name="textfield2" /> <span class="style1">ie. www.google.com</span>
</td>
</tr>
<tr>
<td height="30" align="right"><span class="style1">Photo </span></td>
<td align="left"><input type="file" name="imagefile" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="left"><input name="Submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>';
} else {
if (isset($_POST['Submit'])) {
$file = $_FILES['imagefile']['name'];
$filetype = substr($file,-4);
if ($filetype=="jpeg") {
copy($_FILES['imagefile']['tmp_name'],'uploadimg/'.$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else if ($filetype==".jpg") {
copy($_FILES['imagefile']['tmp_name'],'uploadimg/'.$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else if ($filetype==".gif") {
copy($_FILES['imagefile']['tmp_name'],'uploadimg/'.$_FILES['imagefile']['name']) or die("Could not copy");
echo "<br>Upload Complete";
echo "<br>Name:&nbsp;".$_FILES['imagefile']['name']."";
echo "<br>Size:&nbsp;".$_FILES['imagefile']['size']."";
echo "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";
} else {
echo "<br>Upload Error";
echo "<br>Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
}
}
}
?>
[/code]

Any ideas, im am almost there!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.