westen Posted October 18, 2007 Share Posted October 18, 2007 I am trying to implement a very simple photo upload. For some reason $_FILES['userfile']['tmp_name'] does not exist, could anyone tell me why? Here is the php: [pre] if(isset($_FILES['uploadedfile'])) { $target_path = "./uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; echo ("<p>file name: " . $_FILES['uploadedfile']['name']); echo ("<p>temp name: " . $_FILES['uploadedfile']['tmp_name']); echo ("<p>target path: " . $target_path); } } [/pre] It outputs: There was an error uploading the file, please try again! file name: photo.jpg temp name: target path: ./uploads/photo.jpg Any help would be much appreciated. I am new to php and this has me baffled. Cheers, Westen Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/ Share on other sites More sharing options...
domino1 Posted October 18, 2007 Share Posted October 18, 2007 you probably didnt set up your form correctly (which u didnt include in your post). Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372682 Share on other sites More sharing options...
MadTechie Posted October 18, 2007 Share Posted October 18, 2007 could be write permissions <?php if(isset($_FILES['uploadedfile'])) { $target_path = "./uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); //Add the line below echo (!is_writable($target_path))?"Cannot Write file, Check Permission":""; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; echo ("<p>file name: " . $_FILES['uploadedfile']['name']); echo ("<p>temp name: " . $_FILES['uploadedfile']['tmp_name']); echo ("<p>target path: " . $target_path); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372685 Share on other sites More sharing options...
simcoweb Posted October 18, 2007 Share Posted October 18, 2007 Make sure your permissions are correct also. Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372692 Share on other sites More sharing options...
westen Posted October 18, 2007 Author Share Posted October 18, 2007 My form code looks like this. What would I have to change th permissions on if that is the problem. Apologies for my ignorance. <form action="register.php" enctype="multipart/form-data" method="POST" class="register"> <p><br/></p> <label for="username" class="textb">Username:</label> <input type="text" name="username" size="20" maxlength="10" value="<?php echo $username;?>"/></p> <label for="name" class="textb">Name: </label> <input type="text" class="textb" name="name" size="20" maxlength="20" value="<?php echo $name;?>"/></p> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <label for="upload" class="textb">Upload Photo: </label> <input type="file" name="uploadedfile"/></p> <label for="password" class="textb">Password: </label> <input type="password" name="password" size="20" maxlength="10" value="<?php echo $password;?>"/></p> <label for="confirmpassword" class="textb">Confirm Password: </label> <input type="password" name="confirmpassword" size="20" maxlength="10" value="<?php echo $confirmpassword;?>"/></p> <input type="hidden" name="submitted" value="TRUE"/></p> <div id="standard"> <input type="submit" name="submit" value="Create Account"/></p> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372702 Share on other sites More sharing options...
lewis987 Posted October 18, 2007 Share Posted October 18, 2007 New Upload Code: <?PHP if(isset($_FILES['uploadedfile'])) { $target_path = "./uploads/"; $name = $_FILES['uploadedfile']['name']; $target_path = $target_path . $name; $EXP = explode(".",$name); //Set your own file types here $valid_file_types = array('jpg','jpeg','bmp','gif','png','mp3'); $i = 0; $a = count($valid_file_types); $type = strtolower($EXP[($a -1)]); //Start Validation Check while($i<$a){ if($valid_file_types[$i] == $type){ $VALID = 1; }else{ $VALID = 0; } $i++ } //Check whether file is valid or not if($VALID != 1){ echo('File: '.$name.' is an invalid file type.'); exit; } //Check whether you can copy the file or not. if(copy($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". $name." has been uploaded."; }else{ echo "There was an error uploading the file, please try again!"; echo ("<p>file name: " . $'name); echo ("<p>temp name: " . $_FILES['file']['tmp_name']); echo ("<p>target path: " . $target_path); } ?> REMEMBER CHANGE THE $valid_file_types TO WHATEVER YOU WANT!!! Code UNTESTED Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372716 Share on other sites More sharing options...
MadTechie Posted October 18, 2007 Share Posted October 18, 2007 did you even try the code addition i posted? Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372750 Share on other sites More sharing options...
westen Posted October 18, 2007 Author Share Posted October 18, 2007 Yes I did, thank you. I do not have permission to write to file. The uploads folder lists 0644 under permissions (this is a folder I created and it started out as 0755 but I changed it to be the same as the others which I assumed was writable). I do not even know what this is or should be. This is the first site I have tried to set up. Cheers, Westen Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372760 Share on other sites More sharing options...
westen Posted October 18, 2007 Author Share Posted October 18, 2007 Apologies, I just changed it back and it works fine. It must have been a different problem that made me change it in the first place. Sorry for wasting your time. Maybe I will get around to answering some stupid questions when I know more to pay back the stupidity. Cheers, Nicholas Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372763 Share on other sites More sharing options...
MadTechie Posted October 18, 2007 Share Posted October 18, 2007 Na its cool, i just didn't know what i could offer without more info.. if all is working, can you please click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372766 Share on other sites More sharing options...
westen Posted October 18, 2007 Author Share Posted October 18, 2007 Now I want to cry, it gave me the upload successful message once (but the uploaded file was empty), I didn't change anything and now it is saying I do not have permission again. I am really confused. From what I can tell 0755 is what I want on my directory isn't it? My php files are in /public_html and my upload folder is /public_html/uploads, I cannot see what I am doing wrong. Thank you, NT Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372775 Share on other sites More sharing options...
MadTechie Posted October 18, 2007 Share Posted October 18, 2007 for testing try 0777, if that fails its another problem Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372778 Share on other sites More sharing options...
westen Posted October 18, 2007 Author Share Posted October 18, 2007 bah, must be another problem then Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-372800 Share on other sites More sharing options...
westen Posted October 19, 2007 Author Share Posted October 19, 2007 I am at a total loss, I emailed my web host, they looked at the code and claimed it should work too. Support call closed. God damn it. Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373049 Share on other sites More sharing options...
MadTechie Posted October 19, 2007 Share Posted October 19, 2007 try $target_path = "./uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); to $target_path = "./uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); echo "~$target_path~"; to see the path and check its correct Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373055 Share on other sites More sharing options...
westen Posted October 19, 2007 Author Share Posted October 19, 2007 I get ~./uploads/boo.jpg~ which I think is right, uploads is a directory in the same directory as the php file. Cheers, Nick Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373061 Share on other sites More sharing options...
MadTechie Posted October 19, 2007 Share Posted October 19, 2007 erm.. no $target_path = "uploads/"; for a folder called "uploads" in the current directory Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373067 Share on other sites More sharing options...
westen Posted October 19, 2007 Author Share Posted October 19, 2007 Really? That is what I had originally, i did not think it made any difference but someone on another forum told me to change it. It is still giving the same error anyway. Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373094 Share on other sites More sharing options...
MadTechie Posted October 19, 2007 Share Posted October 19, 2007 Solved ? Quote Link to comment https://forums.phpfreaks.com/topic/73867-solved-upload-file-problem/#findComment-373202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.