Jump to content

file upload problem


jpratt

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!
Link to comment
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]
Link to comment
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?
Link to comment
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
Link to comment
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]
Link to comment
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]
Link to comment
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!
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.