Page 1 of 1
help with a parse error
Posted: Sun Feb 19, 2006 1:31 pm
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\">";
}
?>
Posted: Sun Feb 19, 2006 1:33 pm
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">';
Posted: Sun Feb 19, 2006 1:40 pm
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">';
?>
Posted: Sun Feb 19, 2006 1:43 pm
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">';
Posted: Sun Feb 19, 2006 1:59 pm
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?
Posted: Sun Feb 19, 2006 2:07 pm
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.