The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
File uploading notices
Discuss File uploading notices in the PHP Development forum on Dev Shed. File uploading notices PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 8th, 2012, 06:01 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 9
Time spent in forums: 2 h 40 m 22 sec
Reputation Power: 0
|
|
|
File uploading notices
Hello guys. I am trying to upload images to my website. but i got notice saying:
Notice: Undefined variable: image_temp in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)
Notice: Undefined variable: image_ext in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)
Notice: Undefined variable: album_id in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)
i can see i have files in my database but with no ''album_id '' and 'ext' tables filled in.
Here is the code:
PHP Code:
<?php
if(isset($_FILES['image']) && $_POST['album_id'] ) {
$image_name = $_FILES['image'] ['name'] ;
$image_size = $_FILES ['image'] ['size'];
$image_temp = $_FILES ['image'] ['tmp_name'];
$allowed_ext = array('jpeg','jpg','png','gif');
$image_ext = strtolower(end(explode('.', $image_name)));
$album_id = $_POST['album_id'];
$errors = array();
if(empty($image_name) || empty($album_id)) {
$errors [] = 'Something is missing';
}else {
if(in_array($image_ext, $allowed_ext) ===false) {
$errors [] = 'File type isn\'t allowed';
}
if($image_size > 2097152) {
$errors [] = 'Maximum size is 2 mb';
}
if($album_check($album_id) ===false) {
$errors [] = 'Could not upload to that album';
}
}
}
if(!empty($errors)) {
foreach($errors as $error) {
echo $error, '<br />';
}
}else{
upload_image($image_temp, $image_ext, $album_id); //shows that i have undefined veriables here, why?
}
<form action="" method="POST" enctype ="multypart/form-data">
<p>Choose a file : <input type="file" name="image" /></p>
<p>
Choose an album:</br>
<select name="album_id">
<?php
foreach($albums as $album){
echo '<option value ="'.$album['id'].'">',$album['name'],'</option>';
}
?>
</select>
</p>
<p><input type="submit" value="Upload"/></p>
</form>
<?php
}
?>
here is the function for it:
<?php
function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int) $album_id;
mysql_query("INSERT INTO images VALUES('','".$_SESSION['user_id']."', $album_id , UNIX_TIMESTAMP(), $image_ext)");
$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file ($image_temp, 'uploads/'.$album_id.'/'.$image_file);
}
?>
if you know what my mistake is, let me know. i just cant understand why the veriable are undefined.
thanks for reading
Last edited by requinix : November 8th, 2012 at 12:10 PM.
|

November 8th, 2012, 07:15 AM
|
|
|
It is difficult to see since you didn't use [ PHP ] tags around your code or properly indent it (see ManiacDan's New User Guide) but it looks like your 'upload_image' statement is outside the 'if (isset' block. That means you can execute that code when the page is first loaded and before the form is submitted.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

November 8th, 2012, 11:44 AM
|
|
|
|
depends on where your braces are.. and i dont wanna figure it out
|

November 8th, 2012, 12:11 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
In the interests of getting an answer,
Quote: | Originally Posted by gw1500se It is difficult to see since you didn't use [ PHP ] tags around your code or properly indent it (see ManiacDan's New User Guide) |
Quote: | Originally Posted by paulh1983 depends on where your braces are.. and i dont wanna figure it out |
Fixed.
UkraineIsHere: please read the guide that GW linked to, and next time put [php][/php] tags around your PHP code.
|

November 8th, 2012, 01:15 PM
|
|
|
My original surmise is correct. You are executing 'upload_image' even if those variables have not been set yet. You might try this:
PHP Code:
if (isset($image_temp) && isset($image_ext) && isset($album_id)) {
upload_image($image_temp, $image_ext, $album_id);
}
Alternatively just move all that into the first 'if' block.
|

November 9th, 2012, 01:22 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 9
Time spent in forums: 2 h 40 m 22 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se My original surmise is correct. You are executing 'upload_image' even if those variables have not been set yet. You might try this:
PHP Code:
if (isset($image_temp) && isset($image_ext) && isset($album_id)) {
upload_image($image_temp, $image_ext, $album_id);
}
Alternatively just move all that into the first 'if' block. |
Thank you very much for trying to help me. i dont get any errors now. I now can't upload files to the database. it worked before. even errors [] = ... don't work. i guess i do something completely wrong with my functions. WHat do you think?
Thanks
|

November 9th, 2012, 01:39 PM
|
|
|
|
All my code did was prevent calling 'upload_image' if all the variables were not set. If they are not set when they should be then that is a logic problem you need to resolve. I'm not sure what you mean by "can't upload files to the database." Surely you are not trying to put images into your database are you? In any case I will presume you mean your insert is not working. The first step is to output the generated query string an make sure it is what you expect. Then you should try to copy and paste that string into a command line to see if it works and make adjustments from there.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|