Page 1 of 1

PHP file upload

Posted: Sun Oct 30, 2011 11:17 am
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 :(.
 }//

Re: PHP file upload

Posted: Sun Oct 30, 2011 2:10 pm
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?

Re: PHP file upload

Posted: Tue Nov 15, 2011 1:17 pm
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

Re: PHP file upload

Posted: Tue Nov 15, 2011 1:27 pm
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?