why isnt this php working, i get undefined index...?
I am trying to upload an image to the se开发者_如何学JAVArver.
I hava a form with a couple of inputs... INSIDE this form, I have an Iframe which contains another form for the upload, this because I dont want the original form to submit().
The original form basically:
<form> ...... bla bla bla alot of inputs....<iframe src="pic_upload.html"></iframe></form>
Here is pic_upload.html:
</head>
<body><form name="pic_form" id="pic_form" action="bincgi/imageUpload.php" method="post" target="pic_target"><input name="pic_file" type="file" id="pic_file" size="35" onchange="this.form.submit();"></form><div id="pic_target" name="pic_target" style="width:80px; height:80px;"></div></body>
</html>
Here is imageUpload.php:
<?php
$destination_path = getcwd().DIRECTORY_SEPARATOR;
//echo $destination_path;
$result = 0;
$target_path = $destination_path . basename($_FILES['pic_file']['name']);
if(@move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
$result = 1;
}
?>
I get this error: Undefined index: pic_file on line 5 in imageUpload.php
ALSO, if I get this to work, is there a good way to display the uploaded image inside the target of the form?
Thanks alot! PS: I will add javascript to the tags because maybe thats what I need here!
Don't forget about form attribute
enctype="multipart/form-data"
The form needs to be like this
<form enctype="multipart/form-data" action="__URL__" method="POST">
Check out this PHP.net documentation for more information.
You first need to test if $_FILES
has an item with the key pic_file
. So use the isset
function to do so:
$destination_path = getcwd().DIRECTORY_SEPARATOR;
//echo $destination_path;
$result = 0;
if (isset($_FILES['pic_file'])) {
$target_path = $destination_path . basename($_FILES['pic_file']['name']);
if (@move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
$result = 1;
}
}
精彩评论