Jump to content

file upload problem


clausowitz

Recommended Posts

Hi All,

 

I have a code to upload a file to the server. Somehow the files don't get uploaded, although the record is add to the database.

I have no clue why it doesn't work. The settings of the folder where the docs goto is set to 777.

 

<?php
$uploaddir = "documents/";
$path = $uploaddir.$document;

//This function reads the extension of the file.
$allowedExtensions = array("doc", "pdf", "xls");
function isAllowedExtension($fileName) {
  global $allowedExtensions;
return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($document != ""){ //AS LONG AS A FILE WAS SELECTED...

//compare the size with the maxim size we defined and print error if bigger
$fsize=filesize($_FILES['document']['tmp_name']);
if (($fsize/1024) > $poidsMax*1024)
{
$msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Document did not get posted.</font>';
    include_once 'msgToUser.php';
$errors=1;
}


if(isAllowedExtension($HTTP_POST_FILES['document']['tmp_name'])) {
if(copy($HTTP_POST_FILES['document']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...
		//GET FILE NAME
		$theFileName = $HTTP_POST_FILES['document']['name'];
		//GET FILE SIZE
		$theFileSize = $HTTP_POST_FILES['document']['size'];
		if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
			$theDiv = $theFileSize / 1000000;
			$theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
		} else { //OTHERWISE DISPLAY AS KB
			$theDiv = $theFileSize / 1000;
			$theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
		}


} else { 
$msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>';
    include_once 'msgToUser.php';
$errors=1;
}

} 

}

$name = strip_tags($name, '');
$message = strip_tags($message, '');

$sql = mysql_query("INSERT INTO bulletin_board (posting_date, member_id, title, subject, picture, document) VALUES(now(),'$logOptions_id', '$name','$message', '$image_name', '$theFileName')") or die (mysql_error());

 

Marco

Link to comment
Share on other sites

Agree with the above.

 

You should post code that works stand-alone. Take anything unrelated, and code you know works out.

 

Ideally, we can copy and paste your code and execute it, getting only the errors you need help with.

Link to comment
Share on other sites

Here is all my code:

 

 <?php

$image_name = '';
$theFileName = '';
$name=$_POST['name'];
$message=$_POST['message'];
$image = $HTTP_POST_FILES['image']['name']; 
$document = $HTTP_POST_FILES['document']['name'];
$document = str_replace("#", "No.", $document);
$document = str_replace("$", "Dollar", $document);
$document = str_replace("%", "Percent", $document);
$document = str_replace("^", "", $document);
$document = str_replace("&", "and", $document);
$document = str_replace("*", "", $document);
$document = str_replace("?", "", $document); 
$document = str_replace(" ", "_", $document);
$uploaddir = "documents/";
$path = $uploaddir.$document;
$logOptions_id = $_SESSION['id'];
$poidsMax = ini_get('post_max_size');
// Name and Message required
if (( $name == "") || ( $message == "")) {
print "<p align=center>Please go back to complete all fields!<p>";
echo $name;
}
else {

//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1000"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

//This function reads the extension of the file.
$allowedExtensions = array("doc", "pdf", "xls");
function isAllowedExtension($fileName) {
  global $allowedExtensions;
return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

$errors=0;

if($image<>"") 
{
	//reads the name of the file the user submitted for uploading
	$image=$_FILES['image']['name'];
	if ($image) 
	{
	//get the original name of the file from the clients machine
		$filename = stripslashes($_FILES['image']['name']);
	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
		$extension = strtolower($extension);
	//if it is not a known extension, we will suppose it is an error
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
	//print error message
			$msgToUser = '<br /><br /><font color="#FF0000">You tried to upload a picture with an unknown extension.</font>';
    include_once 'msgToUser.php';
			$errors=1;
		}
		else
		{
	//get the size of the image in bytes
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
$msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Picture did not get posted.</font>';
    include_once 'msgToUser.php';
$errors=1;
}

	//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
	//the new name will be containing the full path where will be stored (images folder)
$newname="pictures/".$image_name;
	//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
$msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>';
    include_once 'msgToUser.php';
$errors=1;
}
	}
}
}

	//If no errors registred, print the success message
if(( $image) && !$errors) 
{
	$msgToUser = '<br /><br /><font color="#FF0000">Message has been posted successfully.</font><p><a href="../index.php">Go Back</a></p>';
    include_once 'msgToUser.php';
}	

if($document != ""){ //AS LONG AS A FILE WAS SELECTED...

//compare the size with the maxim size we defined and print error if bigger
$fsize=filesize($_FILES['document']['tmp_name']);
if (($fsize/1024) > $poidsMax*1024)
{
$msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Document did not get posted.</font>';
    include_once 'msgToUser.php';
$errors=1;
}

if(isAllowedExtension($HTTP_POST_FILES['document']['tmp_name'])) {
if(copy($HTTP_POST_FILES['document']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...
		//GET FILE NAME
		$theFileName = $HTTP_POST_FILES['document']['name'];
		//GET FILE SIZE
		$theFileSize = $HTTP_POST_FILES['document']['size'];
		if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
			$theDiv = $theFileSize / 1000000;
			$theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
		} else { //OTHERWISE DISPLAY AS KB
			$theDiv = $theFileSize / 1000;
			$theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
		}

} else { 
$msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>';
    include_once 'msgToUser.php';
$errors=1;
}

} 

}

$name = strip_tags($name, '');
$message = strip_tags($message, '');

$sql = mysql_query("INSERT INTO bulletin_board (posting_date, member_id, title, subject, picture, document) VALUES(now(),'$logOptions_id', '$name','$message', '$image_name', '$theFileName')") or die (mysql_error());

}

$msgToUser = '<br /><br /><font color="#FF0000">Message has been posted successfully.</font><p><a href="../index.php">Go Back</a></p>';
    include_once 'msgToUser.php';
?> 

 

I know my code is not like a good example of php coding but I am only a beginner.

 

Regards

Marco

Link to comment
Share on other sites

Well, the reason I ask is there are so many outdated tutorials on the internet, and it looks like you managed to find one of them. Unfortunately, if you don't already know what good code looks like, you can't usually tell if code you see in a tutorial is good or not.

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.