help with a parse error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

help with a parse error

Post by jasondavis »

My first use of ajax for a simple file upload it all worked until I tried to add the last part of showing the uploaded file
I get this error
Parse error: parse error, unexpected '[', expecting ']' in /home/myspace/public_html/featuredimageupload.php on line 13

Code: Select all

<?php
	/**
	 * This file uploads a file in the back end, without refreshing the page
	 *  
	 */
	
	if (isset($_POST['id'])) {
		if (!copy($_FILES[$_POST['id']]['tmp_name'], 'featured/uploads/'.$_FILES[$_POST['id']]['name'])) {
			echo '<script> alert("Failed to upload file");</script>';
		}
	}
	else {
		echo "Picture uploaded <img scr=\"featured/uploads/'.$_FILES[$_POST['id']]['name']\" width=\"100\">";
	}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo "Picture uploaded <img scr=\"featured/uploads/'.$_FILES[$_POST['id']]['name']\" width=\"100\">";
to

Code: Select all

echo 'Picture uploaded <img scr="featured/uploads/' . $_FILES[$_POST['id']]['name'] . ' width="100">';
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

thanks for the responce I get a differnet error now though any idea what causes this?

Parse error: parse error, unexpected $ in /home/myspace/public_html/featuredimageupload.php on line 14

here is the full code now

Code: Select all

<?php
	/**
	 * This file uploads a file in the back end, without refreshing the page
	 *  
	 */
	
	if (isset($_POST['id'])) {
		if (!copy($_FILES[$_POST['id']]['tmp_name'], 'featured/uploads/'.$_FILES[$_POST['id']]['name'])) {
			echo '<script> alert("Failed to upload file");</script>';
		}
	}
	else {
			echo 'Picture uploaded <img scr="featured/uploads/' . $_FILES[$_POST['id']]['name'] . ' width="100">';  
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you removed to trailing closing brace '}'

I forgot to put a closing double quote after your variable..

Code: Select all

echo 'Picture uploaded <img scr="featured/uploads/' . $_FILES[$_POST['id']]['name'] . '" width="100">';
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

Errors are gone but it won't do what I need it to

See I have an upload form like this

Code: Select all

<?php
$uploadDirectory = "uploads/";
$uploaderId = 1;


echo '<form id="formName'.$uploaderId.'" method="post" enctype="multipart/form-data" action="featuredimageupload.php" target="iframe'.$uploaderId.'">
							<input type="hidden" name="id" value="1" />
							<span id="uploader'.$uploaderId.'" style="font-family:verdana;font-size:10;">
								Upload Picture to use: <input name="'.$uploaderId.'" type="file" value="'.$uploaderId.'" onchange="return uploadFile(this)" />
							</span>
							<iframe name="iframe'.$uploaderId.'" src="featuredimageupload.php" width="400" height="100" style="display:none"> </iframe>
						</form>';
?>

that for goes to this in an iframe

Code: Select all

<?php
	/**
	 * This file uploads a file in the back end, without refreshing the page
	 *  
	 */
	
	if (isset($_POST['id'])) {
		if (!copy($_FILES[$_POST['id']]['tmp_name'], 'featured/uploads/'.$_FILES[$_POST['id']]['name'])) {
			echo '<script> alert("Failed to upload file");</script>';
		}
	}
	else {
			echo 'Picture uploaded <img scr="featured/uploads/' . $_FILES[$_POST['id']]['name'] . '" width="100">';
}
?>
the problem is it isnt inserting the image name do you know of a way to get that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

move_uploaded_file() should be used.
use basename() on the filename sent by the user if you insist on letting it remain the original name. For safety, you should name the destination file yourself in some manor.
use getimagesize() to determine if the file is an image.
Post Reply