PHP file upload

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

Post Reply
swoffish
Forum Newbie
Posts: 2
Joined: Sun Oct 30, 2011 11:10 am

PHP file upload

Post by swoffish »

When I use this code on my server at school it works fine but when I transfer it to a web host server it gives me a 404 error. I checked all the file permissions and settings and they are all correct. If anyone could help I would highly appreciate it. Thanks ya.

Code: Select all

<?php
top();
$countPic = 0;
$chugger = $_GET["chugger"];
$email = $_GET["email"];
$matcher = $_GET["MatchMe"];
$search = $_GET["search"];
$countPic = $_GET["upload"];

//upload pics stuff//
	$allowed_filetypes = array('.JPG','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 10000000; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = "images/"; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['file']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 //If upload has been attempted
 if($countPic == 1){
	// Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
					?>
						<div id = "bigBox">
							<blockquote>
								File type not allowed 
								<form action="circle.php" method="post" enctype="multipart/form-data">
									<label for="file">Filename:</label>
									<input type="file" name="file" id="file" />
									<br />
									<input type="submit" name="submit" value="Submit" />
								</form>
							</blockquote>
						</div>
					<?php
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['file']['tmp_name']) > $max_filesize)
					?>
						<div id = "bigBox">
							<blockquote>
								File is too large 
								<form action="circle.php" method="post" enctype="multipart/form-data">
									<label for="file">Filename:</label>
									<input type="file" name="file" id="file" />
									<br />
									<input type="submit" name="submit" value="Submit" />
								</form>
							</blockquote>
						</div>
					<?php
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      ?>
						<div id = "bigBox">
							<blockquote>
								chmod 777 the upload file 
								<form action="circle.php" method="post" enctype="multipart/form-data">
									<label for="file">Filename:</label>
									<input type="file" name="file" id="file" />
									<br />
									<input type="submit" name="submit" value="Submit" />
								</form>
							</blockquote>
						</div>
					<?php
 
   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['file']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 }//
Last edited by pickle on Tue Nov 15, 2011 5:40 pm, edited 2 times in total.
Reason: Added opening <?php tag to trigger highlighting
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP file upload

Post by twinedev »

1. Edit the post and use the

Code: Select all

 button to wrap the code properly.

2. What is giving you the 404 error? Trying to even access this script, or accessing files that were uploaded?
swoffish
Forum Newbie
Posts: 2
Joined: Sun Oct 30, 2011 11:10 am

Re: PHP file upload

Post by swoffish »

It is giving me this error after I submit the file i want uploaded. It gives a loading complete then shoots me to a 404 error screen
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP file upload

Post by Celauran »

There were missing braces around most of your if statements.

Code: Select all

<?php
top();
$countPic = 0;
$chugger = $_GET["chugger"];
$email = $_GET["email"];
$matcher = $_GET["MatchMe"];
$search = $_GET["search"];
$countPic = $_GET["upload"];

//upload pics stuff//
$allowed_filetypes = array('.JPG', '.gif', '.bmp', '.png'); // These will be the types of file that will pass the validation.
$max_filesize = 10000000; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = "images/"; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['file']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1); // Get the extension from the filename.
//If upload has been attempted
if ($countPic == 1)
{
// Check if the filetype is allowed, if not DIE and inform the user.
    if (!in_array($ext, $allowed_filetypes))
    {    

        ?>
    <div id = "bigBox">
        <blockquote>
            File type not allowed 
            <form action="circle.php" method="post" enctype="multipart/form-data">
                <label for="file">Filename:</label>
                <input type="file" name="file" id="file" />
                <br />
                <input type="submit" name="submit" value="Submit" />
            </form>
        </blockquote>
    </div>
    <?php
    }
    // Now check the filesize, if it is too large then DIE and inform the user.
    if (filesize($_FILES['file']['tmp_name']) > $max_filesize)
    {    

        ?>
    <div id = "bigBox">
        <blockquote>
            File is too large 
            <form action="circle.php" method="post" enctype="multipart/form-data">
                <label for="file">Filename:</label>
                <input type="file" name="file" id="file" />
                <br />
                <input type="submit" name="submit" value="Submit" />
            </form>
        </blockquote>
    </div>
    <?php
    }
    // Check if we can upload to the specified path, if not DIE and inform the user.
    if (!is_writable($upload_path))
    {    

        ?>
    <div id = "bigBox">
        <blockquote>
            chmod 777 the upload file 
            <form action="circle.php" method="post" enctype="multipart/form-data">
                <label for="file">Filename:</label>
                <input type="file" name="file" id="file" />
                <br />
                <input type="submit" name="submit" value="Submit" />
            </form>
        </blockquote>
    </div>
    <?php
    }
// Upload the file to your specified path.
    if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path . $filename))
        echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
    else
        echo 'There was an error during the file upload.  Please try again.'; // It failed <img src="./images/smilies/icon_sad.gif" alt=":(" title="Sad" />.
}//
Before checking any farther, does this work?
Post Reply