ntesla123 Posted March 9, 2011 Share Posted March 9, 2011 This is my attempt to make an upload script to upload images to my own website. <?php $ch = curl_init("http://*******.com/imgupload.html"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('file'=>"@/home/user/mypic.jpg", 'submit'=>'Submit')); $postResult = curl_exec($ch); curl_close($ch); print "$postResult"; ?> The working upload form I'm trying to post to: <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Why doesn't this work? Quote Link to comment https://forums.phpfreaks.com/topic/230140-uploading-to-my-own-html-form/ Share on other sites More sharing options...
btherl Posted March 9, 2011 Share Posted March 9, 2011 You may need to tell Curl you are doing a post, by setting CURLOPT_POST to 1. Quote Link to comment https://forums.phpfreaks.com/topic/230140-uploading-to-my-own-html-form/#findComment-1185239 Share on other sites More sharing options...
ntesla123 Posted March 9, 2011 Author Share Posted March 9, 2011 OK, thanks. Now I have done it, but it still doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/230140-uploading-to-my-own-html-form/#findComment-1185241 Share on other sites More sharing options...
Mahngiel Posted March 9, 2011 Share Posted March 9, 2011 Chances are, the photos ARE being uploaded. Just to your tmp file location. I'll show you my process. I have a very buggy, not yet safe upload form that I've written during the design phase of my site, but it does indeed work. You're free to use it, and I'll describe what it does along the way. upload.php <?php include('header.php');echo '<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"><br /> <label for="file">toast:</label><br /> <input type="file" name="upload" /><br /> <input type ="text" name ="name" /><br /> <input type="submit" name="submit" value="Upload File" /> </form>'; ?> assigned variables from my all-inclusive script (header.php) //Defines the directory for file uploads $avatar_dir = 'avatars/'; //Since files are uploaded with a standard definitions, let's break it down to easy calls $tmp_name = $_FILES['upload']["tmp_name"]; $avatarftype = $_FILES['upload']["type"]; //explode is needed to separate "image/jpg or png or gif" into just /jpg or /png or /gif.. Then replace the slash w/ a dot $fext = explode("image", $avatarftype); $fext = str_replace("/", ".", $fext); //this is the result of the extension strip and replace $ext = $fext[1]; //The name you've chosen, plus it's extension $name = ($_POST['name']; $mypic = ($name.$ext); the processing script (upload_file.php): <?php include('header.php'); // Determine file types and size. This allows for only gifs, jpgs (both IE and other browser safe) and pngs under 60 kb if ((($_FILES['upload']["type"] == "image/gif") || ($_FILES['upload']["type"] == "image/jpeg") || ($_FILES['upload']["type"] == "image/pjpeg") || ($_FILES['upload']["type"] == "image/png")) && ($_FILES['upload']["size"] < 60000)){ //Notify if error if ($_FILES['upload']["error"] > 0){ echo "Return Code: " . $_FILES[$name]["error"] . "<br />"; }else{ // Upload it to server and log into database echo $mypic; move_uploaded_file($tmp_name, $avatar_dir.$mypic); //if you chose to store it in the database mysql_query("INSERT INTO 'yourtable' (file) VALUE (($mypic); } //Kick out }else{ echo 'Invalid file,'; } ?> I heavily modified my own personal scripts to provide this to you. I am pretty sure it works, but it's untested. The intent here is to help you learn while giving you a living example. Quote Link to comment https://forums.phpfreaks.com/topic/230140-uploading-to-my-own-html-form/#findComment-1185242 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.