Jump to content

can't get image to upload


contra10

Recommended Posts

hey im trying to make this code work for an image upload

 

<?php
/*configuration */

$root= "C:\\wamp\\www\\imagealbum\\";
$urlroot = "http://localhost/images/";
$max_width= 420;
$max_height = 600;
$overwrite_images = false;

$target_dirs = array("article", "banners");


if(!function_exists(getimagesize)){
	die("getimagesize() required.");
}

$location = strval($_POST['location']);
$newname = strval($_POST['newname']);
$upfile = $_FILES['upfile']['tmp_name'];
$upfile_name = $_FILES['upfile']['name'];

if($newname){
$newname= preg_replace('/[^A-Za-z0-9_.-]/', '', $newname);
}else{
$newname = preg_replace('/[^A-Za-z0-9_.-]/', '',$upfile_name);
}

if (!in_array($location, $target_dirs)){
die("invalid target directory.");
}else{
$urlroot .="/location";
			}
if (!upfile){
die("no file for upload. ");
}

$file_types = array(
	"image/jpeg"	=> "jpg",
	"image/pjpeg"	=>"jpg",
	"image/gif"		=> "gif",
	"image/png"		=> "png",

	);
	$width = null;
	$height = null;

	$img_info = getimagesize($upfile);
	$upfile_type = $img_info["mime"];
	list ($width, $height, $t, $attr) = $img_info;

	if (!$file_types[$upfile_type]){
	die("Image must be in JPEG, GIF, or PNG format");
	}else{
	$file_suffix = $file_types[$upfile_type];
						}

if($width > $max_width || $height > $max_height){
die ("size $width x $height exceeds maximum $max_width x $max_height.");
}

$newname = preg_replace('/\.(jpe?g|gif|png)$/i', "");
$newname .= $file_suffix;
$new_fullpath = "$root/$location/$newname";

if((!overwrite_images) && file_exists($new_fullpath)){
die("file exists; will not overwrite.");
}

if(!copy($upfile,$new_fullpath)){
die("copy failed.");
}

$image_url = "$urlroot/$newname";
print "HTML for image:</strong><br><textarea cols=\"80\" rows\"4\">";
print "<img src\"image_url\" $attr alt=\"$upfile_name\" border=\"0\"/>";
print "</textarea><br>";
print '<a href="http://localhost/photoupload/"> Upload another image></a>';
?>

 

instead i get these errors

 

Warning: Wrong parameter count for preg_replace() in C:\wamp\www\image_process.php on line 62

Warning: copy(file:///C|/wamp/www/images//article/jpg) [function.copy]: failed to open stream: Invalid argument in C:\wamp\www\image_process.php on line 70
copy failed.

Link to comment
https://forums.phpfreaks.com/topic/139400-cant-get-image-to-upload/
Share on other sites

thxs that solved the first part but i still get this error

 

Warning: copy(C:\wamp\www\imagealbum\/article/n28289389880_7562jpg) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\image_process.php on line 70
copy failed.

 

the directory or file does exists

if(!move_uploaded_file ($upfile,$new_fullpath)){
die("copy failed.");

 

for some reason it still doesnt' work i get

Warning: move_uploaded_file(C:\wamp\www\imagealbum\/article/world2png) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\image_process.php on line 70

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpCA.tmp' to 'C:\wamp\www\imagealbum\/article/world2png' in C:\wamp\www\image_process.php on line 70
copy failed.

}

I noticed the slashes too.  Another thing that bothers me about the filename and path:

 

C:\wamp\www\imagealbum\/article/world2png

 

Notice there is no '.' between the file name and file extension.

 

The problem is your $root variable contents. 

 

You have a preg_replace that wants to use the '/' slash.  However, in Windows it uses the '\'.  To make it accessible on the website we need to use the '/'.  Process of elimination is to fix the $root variable contents.

 

So try this for $root:

 

$root="C:/wamp/www/imagealbum";

 

In WAMP the httpd.conf utilizes the '/' in the DocumentRoot.

 

To fix the '.' missing between the file name and the extension....somebody please recheck the preg_replace :S

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.