Jump to content

PHP File Upload Not Working Properly


klturi421

Recommended Posts

I have been searching for an answer for several days but have not yet resolved my problem. Essentiallty I started out looking for an image upload script then slowly went towards just a basic upload script. Each time I try a new script I am getting the same error. My folder permissions are set, file uploads is enabled, safe mode is disabled.

 

As of right now my page is giving me an error that says 'Failed to open stream'

 

My existing code:

<?php 
$target = "images/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
?> 

<form enctype="multipart/form-data" action="uploader.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form> 

 

You for testing purposes I have made my temporary upload page available for you to view and help disgnose the issue: http://www.sophienoelle.me/uploader.php

 

also you can find the phpinfo at http://www.sophienoelle.me/phpinfo.php

 

If you need further information than what I have supplied please let me know and I will be available to provide what you are requesting.

 

If you have a suggestion I will gladly take it. Any steps I can get to uploading images the better.

Link to comment
Share on other sites

Im not sure how to do that on a GoDaddy shared server. I just tried uploading a php5.ini changing the tmp dir but havent seen any change in phpinfo yet. Am I missing something? Should I be able to access the tmp folder within the FTP client or is there another way to go about it?

 

 

Link to comment
Share on other sites

Several of your "checks" were not being processed.  See if this helps.

<?php 
if(isset($_POST['submit'])){
$target = "images/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1;
  
$uploaded_size=($_FILES['uploaded']['size']);
//This is our size condition.  Many hosts limit to 2mb by default 
if ($uploaded_size > 2000000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
}
  function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
} 
$uploaded_type=getExtension($_FILES['uploaded']['name']);
//This is our limit file type condition 
if ($uploaded_type =="php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
$ok=2; 
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; 
} 
else 
{
$ok=0; 
echo "Sorry, there was a problem uploading your file."; 
} 
}
}//if(isset($_POST['submit']))
if($ok==1){echo "Size Limit 2mb";} 
?> 
<form enctype="multipart/form-data" action="uploader.php" method="post"> 
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form> 

Link to comment
Share on other sites

Thanks Drummin, I was able to get it to work just fine with your post. I recent discovered a script from a website that will upload images and resize them and stick them in seperate folders. I have written the code and debugged it sort of. At this point each time I click upload it returns an error that says that I need to select a file even if there was a file present.

 

The code I used:

<?php
//zeronese.net
function create_thumbnail($source,$destination, $thumb_width) {
	$size = getimagesize($source);
	$width = $size[0];
	$height = $size[1];
	$x = 0;
	$y = 0;
	if($width> $height) {
		$x = ceil(($width - $height) / 2);
		$width = $height;
	} elseif($height> $width) {
		$y = ceil(($height - $width) / 2);
		$height = $width;
	}
	$new_image = imagecreatetruecolor($thumb_width,$thumb_width)
		or die('Cannot Initialise new GD image stream');
	$extention = get_image_extension($source);
	 if($extension== 'jpg' || $extension=='jpeg')
		$image = imagecreatefromjpeg($source);
	 if($extension== 'gif')
		$image = imagecreatefromgif($source);
	 if($extension== 'png')
		$image = imagecreatefrompng($source);

	imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
	if($extension=='jpg' || $extension=='jpeg')
		imagejpeg($new_image,$destination);
	if($extension=='gif')
		imagegif($new_image,$destination);
	if($extension=='png')
		imagepng($new_image,$destination);
	}
	//zeronese.net
	//zeronese.net get_image_extension($name)
	function get_image_extension($name) {
		$name = strtolower($name);
		$i = strrpos($name,".");
		if (!$i) { return"";}
			$l = strlen($name) - $i;
		$extension = substr($name,$i+l,$l);
		return $extension;
	}//end seronese.net get_image_extension($name)

function random_name($lentgh) {
	$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
	$name = "";

	for ($i = 0; $i < $length; $i++) {
	$name .= $characters[mt_rand(0, strlen($characters) - 1)];
	}
	return "image-".$name;
	}

//you can change this to the directory that will hold your images
	$images_location = "/images/";
//this is also where we are going to store our thumbnails
	$thumbs_location = "/images/thumbs/";
	$thumb_width = 100; //width in pixels
	$maximum_size = 5000000;//maximum size in kb

//a message to show on our action, lets start by a help label
	$results = "Click browse to locate the image you want to upload!";

	if($_POST){
	//check if the image source is empty
	if($_FILES['image']['name'] == ""){
		$results = "Image source can not be empty/
				Please Click the 'Browse' button,
				locate an image then click the 'Upload Image' button!";
	}
	//if an image source is entered, process the upload
	else{
		$size=filesize($_FILES['image']['tmp_name']);
		$filename = stripslashes($_FILES['image']['name']);
		$extension = get_image_extension($filename);
		if($size > $maximum_size){
			$results = "your file size exceeds the maximum size limit!
				Please Try Again!";
	}
	else
	// allow only jpg, jpeg, gif, and png files
		if(($extension != "jpg") &&
			($extension != "jpeg") &&
			($extension != "png") &&
			($extension != "gif")) {
			$results = 'Images can only be with jpg,jpeg,png or gif extension.
				Please try again!</center>';
		}
		else{
			//generate a random name to avoid deleting duplicates
			$image_random_name= random_name(15).".".$extension;
			$copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
			if (!$copy){
				$results = "Error while uploading image! Please try again!";
		}
		else{
				create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width);
				$results = "Image has been uploaded";
			}
		}
	}
}

?>

<form method="POST" enctyp="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<input type="file" name="image">
<input type ="submit" value="Upload Image">
</form>
<?php echo $results; ?>

 

The website where I got the code :  http://www.zeronese.net/knowledge-base/questions/1060/Image-Uploading-With-Auto-Thumbnails-Using-PHP

 

The code available on the website is not copy and pasteable, its all images. I was hoping using firebug or chrome would return some errors in console but it does nothing.

 

EDIT: My upload page can be found at http://sophienoelle.me/uploader.php

 

 

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.