trouble uploading images w/ form

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
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

trouble uploading images w/ form

Post by someguyhere »

I've looked at several tutorials on uploading files to a server via php but I can't seem to get it to work. Can someone point me to a decent tutorial for this? I need to upload two images to a particular folder, and I'll be adding additional fields (text) later.

I've done far more complex things than this, but for some reason, I seem to have the logic of a drunk toddler on this one :banghead:

Any help is greatly appreciated.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: trouble uploading images w/ form

Post by litebearer »

do you have some code to show us where you experiencing problems?
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: trouble uploading images w/ form

Post by someguyhere »

Here's what I've got so far. It's modified from something I found online, but even if/when I do get it working, the other issue is that I will need to add one more image upload to this form which I can't see how to do.

Code: Select all

<?php

if (isset($_POST["logo"])){

$target = "/wp-content/plugins/wp-members-pages/images/"; 
 $target = $target . basename( $_FILES['logo']['name']) ; 
 $ok=1; 
 
 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 
 
 //This is our limit file type condition 
 if ($uploaded_type =="text/php") 
 { 
 echo "No PHP files<br>"; 
 $ok=0; 
 } 
 
 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 
 
 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['logo']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 

}

if (empty($_POST["logo"])){

	echo '<form action="" method="post" >';
	echo '<input name="logo" type="file" />';
	echo '<input type="submit" value="Upload" />';
	echo '</form>';

}

?>
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: trouble uploading images w/ form

Post by litebearer »

Your form tag needs

Code: Select all

enctype="multipart/form-data"
in it.

see...
http://www.tizag.com/phpT/fileupload.php
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: trouble uploading images w/ form

Post by someguyhere »

I used the one you linked to with some modifications. Still no dice though. When I click submit, all it will do for me is refresh to the form again, but it doesn't give me a message saying either that the file has been uploaded or has failed to upload.

Code: Select all

<?php
if (isset($_POST["uploadedfile"])){

// Where the file is going to be placed 
$target_path = "/test/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$target_path = "/test/";

$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!";
}

}

if (empty($_POST["uploadedfile"])){

	echo '<form enctype="multipart/form-data" action="" method="POST">';
	echo '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
	echo '<input name="uploadedfile" type="file" />';
	echo '<input type="submit" value="Upload File" />';
	echo '</form>';

}

?>
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: trouble uploading images w/ form

Post by someguyhere »

Anyone?
Post Reply