doforumda Posted February 26, 2010 Share Posted February 26, 2010 i want to upload file with php. my code is below. I describe my problem below the whole code upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="uploadProcess.php" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" /><p> <input type="submit" name="submit" value="Upload" /> </form> </body> </html> uploadProcess.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php include("getExtension.php"); include("imageResizer.php"); $name = $_FILES['myfile']['name']; $type = $_FILES['myfile']['type']; $size = $_FILES['myfile']['size']; $temp_name = $_FILES['myfile']['tmp_name']; $error = $_FILES['myfile']['error']; echo $temp_name."<br>"; echo $name."<br>"; $ext = getExtension($name); $ext = strtolower($ext); echo $ext."<br>"; //die(); if($error == 0) { $location = "photos/".$name; if($size <= 1000000) { if($ext == "png" || $ext == "jpeg" || $ext == "jpg" || $ext == "gif") { if(!file_exists($location)) { //move_uploaded_file($temp_name, $location); image($name,$ext); echo "File uploaded successfully."; } else die("This file is already exists."); } else die("This format is not supported. Only png, jpeg, jpg and gif images are supported."); } else die("File size is too big."); } else die("There is an error uploading code: $error"); ?> </body> </html> imageResizer.php <?php function image($name, $ext) { // The file $filename = $name; $percent = 0.05; $save = "photos/".$name; // Content type header('Content-type: image/'.$ext); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; if($width > 60 && $height > 60) { // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } else { $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); } // Output imagejpeg($image_p, $save, 100); } //image('test.jpg','jpg'); ?> here the problem is in upload Process.php when i call function image($name,$ext); the it does not save my file on the server but when i call this function like this image('image.jpg','jpg'); then it saves this file on the server. how can i fix this so it saves file when i pass variables as parameters? Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/ Share on other sites More sharing options...
Adam Posted February 26, 2010 Share Posted February 26, 2010 Have you tried a var_dump on the $name and $ext variables before you pass them to the function? Since the $name variable looks like it should be correct, my guess would be the problem's with getExtension() - but we're unable to see the code. Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018384 Share on other sites More sharing options...
doforumda Posted February 26, 2010 Author Share Posted February 26, 2010 ok here is getExtension.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018398 Share on other sites More sharing options...
Adam Posted February 26, 2010 Share Posted February 26, 2010 Why have you add HTML header and footer tags to getExtension.php? Anyway not the cleanest code but does seem to work. Did you try the var_dump? Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018405 Share on other sites More sharing options...
doforumda Posted February 26, 2010 Author Share Posted February 26, 2010 well where should i user var_dump i have never used this before. and secondly should i remove HTML header and footer? Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018410 Share on other sites More sharing options...
Adam Posted February 26, 2010 Share Posted February 26, 2010 Yeah remove the HTML from getExtension.php. Just after $ext = getExtension($name);, add in: var_dump($name); var_dump($ext); Just to check the values are what you're expecting (remove them after). Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018411 Share on other sites More sharing options...
doforumda Posted February 26, 2010 Author Share Posted February 26, 2010 i did that but still not working Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018417 Share on other sites More sharing options...
Adam Posted February 26, 2010 Share Posted February 26, 2010 In what way does it not work.. blank screen, errors? Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018423 Share on other sites More sharing options...
doforumda Posted February 26, 2010 Author Share Posted February 26, 2010 it displays this http://localhost/fileupload/uploadProcess.php Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018428 Share on other sites More sharing options...
Adam Posted February 26, 2010 Share Posted February 26, 2010 I'd make sure you have errors displayed. Add this to the start of your code: error_reporting(E_ALL); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/193426-fileupload-help/#findComment-1018458 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.