Jump to content

Recommended Posts

I'm in the process of making an ACP, which include image uploading. Since it's been a long time since I did something like this in PHP (I'm mostly a RoR programmer now >_>) I followed a tutorial.

 

Index, with form (under "Add a Project"): http://rfcy.net/residential/beta/index.txt

Page the form links to: http://rfcy.net/residential/beta/add.txt

 

I'm sure the problem is obvious, hopefully easy to fix. Basically, the data goes into the MySQL database, but the image URL doesn't... it remains blank.

 

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/155547-solved-image-upload-not-working/
Share on other sites

you have an extra curly bracket. change this:

 

function upload($image) {
$image=$_FILES['image']['name'];
if ($image) {
	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) {
		$size=filesize($_FILES['image']['tmp_name']);
		$image_name=time().'.'.$extension;
		$newname="images/".$image_name;
		$copied = copy($_FILES['image']['tmp_name'], $newname);
	}
}
return $newname;
}

 

to this

function upload($image) {
$image=$_FILES['image']['name'];
if ($image) {
	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) {
		$size=filesize($_FILES['image']['tmp_name']);
		$image_name=time().'.'.$extension;
		$newname="images/".$image_name;
		$copied = copy($_FILES['image']['tmp_name'], $newname);

}
return $newname;
}

Actually...you just took out a bracket that he needed, his code won't parse now.

 

@NaveAdair: I'm not sure exactly what your code is supposed to do.  To upload files, you need to be using the $_FILES array, not the $_POST array.  Why are you passing $_POST['pic1'] to upload()?

Actually...you just took out a bracket that he needed, his code won't parse now.

 

@NaveAdair: I'm not sure exactly what your code is supposed to do.  To upload files, you need to be using the $_FILES array, not the $_POST array.  Why are you passing $_POST['pic1'] to upload()?

 

oops your right... missed the last bracket

Actually...you just took out a bracket that he needed, his code won't parse now.

 

@NaveAdair: I'm not sure exactly what your code is supposed to do.  To upload files, you need to be using the $_FILES array, not the $_POST array.  Why are you passing $_POST['pic1'] to upload()?

 

I'm not sure myself. >_> Might that be the solution to my problem?

 

EDIT: Ugh, I might need to find a better tutorial, since the one I went off just threw code at me... <_<

 

EDIT 2:

 

Tried this

 

function upload($image) {
if ($image) {
	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) {
		$size=filesize($_FILES['image']['tmp_name']);
		$image_name=time().'.'.$extension;
		$newname="images/".$image_name;
		$copied = copy($_FILES['image']['tmp_name'], $newname);
	}
	return $newname;
}
}

 

$pic1 = upload($_FILES['pic1']['name']);
$pic2 = upload($_FILES['pic2']['name']);
$pic3 = upload($_FILES['pic3']['name']);
$pic4 = upload($_FILES['pic4']['name']);
$pic5 = upload($_FILES['pic5']['name']);
$pic6 = upload($_FILES['pic6']['name']);
$pic7 = upload($_FILES['pic7']['name']);
$pic8 = upload($_FILES['pic8']['name']);
$pic9 = upload($_FILES['pic9']['name']);
$pic10 = upload($_FILES['pic10']['name']);
$pic11 = upload($_FILES['pic11']['name']);
$pic12 = upload($_FILES['pic12']['name']);

 

 

Didn't work. Worth a try, I guess. >_>

Also tried:

 

function upload($image) {
if ($_FILES[$image]['name']) {
	$filename = stripslashes($_FILES[$image]['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension == "jpg") || ($extension == "jpeg") || ($extension == "png") || ($extension == "gif")) {
		$image_name=time().'.'.$extension;
		$newname="images/".$image_name;
		$copied = copy($_FILES[$image]['tmp_name'], $newname);
	}
	return $newname;
}
}

 

$pic1 = upload("pic1");
$pic2 = upload("pic2");
$pic3 = upload("pic3");
$pic4 = upload("pic4");
$pic5 = upload("pic5");
$pic6 = upload("pic6");
$pic7 = upload("pic7");
$pic8 = upload("pic8");
$pic9 = upload("pic9");
$pic10 = upload("pic10");
$pic11 = upload("pic11");
$pic12 = upload("pic12");

 

 

 

 

Now I get a URL in the database, but the image doesn't actually copy over, and the error:

 

Warning: copy(images/1240605845.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25

instead of using the copy function, try using the move_uploaded_file function so change the following

$copied = copy($_FILES[$image]['tmp_name'], $newname);

to

$copied = move_uploaded_file($_FILES[$image]['tmp_name'], $newname);

 

hope that helped

That doubled the amount of error messages, for better or worse.

 

Warning: move_uploaded_file(images/1240606194.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php327.tmp' to 'images/1240606194.jpg' in C:\xampp\htdocs\Residential\admin\projects\add.php on line 25

two things.

 

Make sure the directory you are ttrying to move the files to exists.

something like:

ff (!file_exists('images/')) {
mkdir('images');
}

this will check if the dir exists, and if not it will make it.

 

now make sure that your script has access to moving, and creating folders/files in your server. It has to have its permissions set to 777 i believe.

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.