AdRock Posted September 27, 2008 Share Posted September 27, 2008 I am having a nightmare getting an image to upload. On the same page as the form i have this $picture=($_FILES['image']['name']); and to call the function that does the insert i use this $result = content_image_insert('events','title','content','eventdate','photo','alternate',$content_title, $content,$date,$picture,'alternate'); I have tried to upload an image but it doesn't upload. What could be causing this error? I think everything else will work as long as i can get the image to upload so it must be something to do with $_FILES Here is the functions that matter. if need be i'll post everything function content_image_insert($percent,$new_width,$new_height,$width,$height) { global $percent,$new_width,$new_height,$width,$height; upload_image($percent,$new_width,$new_height,$width,$height); } function upload_image($picture,$percent,$new_width,$new_height,$width,$height) { global $percent,$new_width,$new_height,$width,$height; //This is the directory where images will be saved $th_file_name = "../images/large/" . $picture; $imagepath = $_FILES['image']['tmp_name']; // Load image $image = open_image($imagepath); if ($image == false) { return ('<strong>You uploaded an invalid image. Please go back and try again.</strong>'); } else { // Get original width and height $width = imagesx($image); $height = imagesy($image); // Percentage? if (!empty($percent)) { $percent = floatval($percent); $percent = $percent/100; $new_width = $width * $percent; $new_height = $height * $percent; // New width? Calculate new height } elseif (!empty($new_width)) { $new_width = floatval($new_width); $new_height = $height * ($new_width/$width); // New height? Calculate new width } elseif (!empty($new_height)) { $new_height = floatval($new_height); $new_width = $width * ($new_height/$height); // New height and new width } elseif (!empty($height) AND !empty($width)) { $new_height = floatval($_POST['height']); $new_width = floatval($_POST['width']); } // Resample $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Display resized image imagejpeg($image_resized,$th_file_name,100); return "Correct"; } } function open_image ($file) { # JPEG: $im = @imagecreatefromjpeg($file); if ($im !== false) { return $im; } # GIF: $im = @imagecreatefromgif($file); if ($im !== false) { return $im; } # PNG: $im = @imagecreatefrompng($file); if ($im !== false) { return $im; } # GD File: $im = @imagecreatefromgd($file); if ($im !== false) { return $im; } # GD2 File: $im = @imagecreatefromgd2($file); if ($im !== false) { return $im; } # WBMP: $im = @imagecreatefromwbmp($file); if ($im !== false) { return $im; } # XBM: $im = @imagecreatefromxbm($file); if ($im !== false) { return $im; } # XPM: $im = @imagecreatefromxpm($file); if ($im !== false) { return $im; } # Try and load from string: $im = @imagecreatefromstring(file_get_contents($file)); if ($im !== false) { return $im; } return false; } Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/ Share on other sites More sharing options...
wildteen88 Posted September 27, 2008 Share Posted September 27, 2008 Problem I see is you're passing your content_image_insert function 10 parameters on this line: $result = content_image_insert('events','title','content','eventdate','photo','alternate',$content_title, $content,$date,$picture,'alternate'); Where as here: function content_image_insert($percent,$new_width,$new_height,$width,$height) { You're defining the function, but it only accepts 5 parameters. Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-651976 Share on other sites More sharing options...
AdRock Posted September 27, 2008 Author Share Posted September 27, 2008 I forgot to take that out. What the other paramters are for is inserting some info into a database but because that works i took it out. I am not sure but i think it has to be to do with the $_FILES. I'm not sure how i can pass a file to a fuinction to do something with it like resize it. Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-651994 Share on other sites More sharing options...
wildteen88 Posted September 28, 2008 Share Posted September 28, 2008 $_FILE is a superglobal variable (just like $_GET, $_POST, $_SESSION etc). You do not need to anything in order for superglobal variables to work within a function. Can you post your form here too Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-652290 Share on other sites More sharing options...
AdRock Posted September 28, 2008 Author Share Posted September 28, 2008 The form code: I have taken out all the database stuff as that not needed. This is the bare image upload bit <?php require_once('php/script.php'); // Validate the form if ( isset ($_POST['submit']) ) { $picture=($_FILES['image']['name']); $percent = check_input(trim($_POST['percent'])); $new_width= check_input(trim($_POST['new_width'])); $new_height = check_input(trim($_POST['new_height'])); $width = check_input(trim($_POST['width'])); $height = check_input(trim($_POST['height'])); if(empty($pic)) { $result = content_insert('events','title','content','eventdate',$content_title, $content, $date); } else { $result = content_image_insert('events','title','content','eventdate','photo','alternate',$content_title, $content,$date,'alternate'); } if ($result == 'Correct') { header('Location: insert-success.php'); } else { $insert_error = $result; } } // Include the header html require_once("header.inc.php"); echo ( "<h1>Add an Article</h1>"); // If there was an error requesting a new password, display the error message. Could be email address not found if (isset($insert_error)) { echo "There was an error: ".$insert_error; } ?> <form enctype="multipart/form-data" id="adminform" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <p>Please enter a title for the general article. <input class="form" type="text" title="Please enter a title for the general article" name="title" size="30"/></p> <p>Please enter the content for the general article. <textarea class="form" title="Please enter the content for the general article" name="content" rows="25" cols="30"></textarea></p> <fieldset> <legend><b>Image Dimensions</b></legend> <p><label class="adminlabel" for="image">Image:</label><input type="file" name="image" /></p> <p><label class="adminlabel" for="alternate">Image description:</label><input type="text" size="20" name="image" /></p> <p><label class="adminlabel" for="percent">Resize to:</label><input type="text" name="percent" size="2" /> % (percentage)</p> <p><label class="adminlabel" for="new_width">OR new width:</label><input type="text" name="new_width" size="2" /> pixels (height will be calculated automatically)</p> <p><label class="adminlabel" for="new_height">OR new height:</label><input type="text" name="new_height" size="2" /> pixels (width will be calculated automatically)</p> <p><label class="adminlabel" for="width">OR new height and new width:</label><input type="text" name="width" size="2" /> pixels</p> <p><label class="adminlabel" for="height"> </label><input type="text" name="height" size="2" /> pixels</p> </fieldset> <p><input class="submit-button" style="margin-left:0" name="submit" type="submit" value="" /><input class="reset-button" style="margin-left:2px" type="reset" value="" /></p> </form> <?php // Include the footer html require_once("footer.inc.php"); ?> and script.php with the functions: function content_image_insert($percent,$new_width,$new_height,$width,$height) { global $percent,$new_width,$new_height,$width,$height; $uploaded = upload_image($percent,$new_width,$new_height,$width,$height); } function upload_image($percent,$new_width,$new_height,$width,$height) { global $percent,$new_width,$new_height,$width,$height; //This is the directory where images will be saved $th_file_name = "../images/large/" . $_FILES['image']['name']; $imagepath = $_FILES['image']['tmp_name']; // Load image $image = open_image($imagepath); if ($image == false) { return ('<strong>You uploaded an invalid image. Please go back and try again.</strong>'); } else { // Get original width and height $width = imagesx($image); $height = imagesy($image); // Percentage? if (!empty($percent)) { $percent = floatval($percent); $percent = $percent/100; $new_width = $width * $percent; $new_height = $height * $percent; // New width? Calculate new height } elseif (!empty($new_width)) { $new_width = floatval($new_width); $new_height = $height * ($new_width/$width); // New height? Calculate new width } elseif (!empty($new_height)) { $new_height = floatval($new_height); $new_width = $width * ($new_height/$height); // New height and new width } elseif (!empty($height) AND !empty($width)) { $new_height = floatval($_POST['height']); $new_width = floatval($_POST['width']); } // Resample $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Display resized image imagejpeg($image_resized,$th_file_name,100); return "Correct"; } } function open_image ($file) { # JPEG: $im = @imagecreatefromjpeg($file); if ($im !== false) { return $im; } # GIF: $im = @imagecreatefromgif($file); if ($im !== false) { return $im; } # PNG: $im = @imagecreatefrompng($file); if ($im !== false) { return $im; } # GD File: $im = @imagecreatefromgd($file); if ($im !== false) { return $im; } # GD2 File: $im = @imagecreatefromgd2($file); if ($im !== false) { return $im; } # WBMP: $im = @imagecreatefromwbmp($file); if ($im !== false) { return $im; } # XBM: $im = @imagecreatefromxbm($file); if ($im !== false) { return $im; } # XPM: $im = @imagecreatefromxpm($file); if ($im !== false) { return $im; } # Try and load from string: $im = @imagecreatefromstring(file_get_contents($file)); if ($im !== false) { return $im; } return false; } I used to have this working when it was all in one file a couple years ago but i am rewriting my code so i know it works. I just seem to get the file to upload Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-652304 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2008 Share Posted September 28, 2008 Start by adding the following lines immediately after your first opening <?php tag on your form processing page - ini_set ("display_errors", "1"); error_reporting(E_ALL); echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-652332 Share on other sites More sharing options...
AdRock Posted September 28, 2008 Author Share Posted September 28, 2008 I get this POST:Array ( [title] => dfgdf [content] => dfgdsgsdg [image] => [percent] => 50 [new_width] => [new_height] => [width] => [height] => [submit] => ) FILES:Array ( [image] => Array ( [name] => title1.jpg [type] => image/jpeg [tmp_name] => /tmp/phph9nb7m [error] => 0 => 23977 ) ) Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-652420 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2008 Share Posted September 28, 2008 That shows that the submit button is set and the $_FILES array contains a valid uploaded file (your code has no error checking to make sure before it proceeds to access the data) so it should be a simple matter for you to determine where in your code that values are present and not present to find where the problem is. Link to comment https://forums.phpfreaks.com/topic/126084-problems-with-image-upload/#findComment-652423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.