php - Function doesn't move image but returns info correctly -
i'm tryin upload 2 images seems move_uploaded_file() doesn't move file, no errors have been generated.
// upload image function upload_image($tmp,$name) { $ext = explode('.',$name); if (preg_match('~(jpeg|jpg|gif|png)$~i', $name)) { $ext = end($ext); $name = substr(md5(rand()), 0, 10).'.' . $ext; $upload_dir = '/music/uploads/' . $name; echo $tmp; move_uploaded_file($tmp,'/home/shadne/public_html'.$upload_dir); return $upload_dir; } else { throw new errorexception('file type not allowed'); } }
file/folder permission 0777, used images/new before using /uploads
edit 1
code handles uploading , checkin
<?php require_once '../config.php'; require 'includes/functions.php'; if (!empty($_files['artist_profile_image'])) { $profile_image_name = $_files['artist_profile_image']['name']; $profile_image_tmp = $_files['artist_profile_image']['tmp']; $profile_image_dir = upload_image($profile_image_tmp, $profile_image_name); } if (!empty($_files['artist_thumb'])) { $thumb_name = $_files['artist_thumb']['name']; $thumb_tmp = $_files['artist_thumb']['tmp']; $thumb_dir = upload_image($thumb_tmp, $thumb_name); } if (empty($_post) === false) { $required_fields = array('artist_name', 'artist_thumb', 'artist_profile_image', 'birthday'); foreach ($_post $key => $value) { if (empty($value) && in_array($key, $required_fields) === true) { $errors[] = 'fields marked asterstrik required'; break 1; } } } if (isset($_get['success'])) { echo 'you\'ve added new artist.'; } else { if (empty($_post) === false && empty($errors) === true) { $artist_data = array( 'artist_name' => $_post['artist_name'], 'artist_thumb' => $thumb_dir, 'artist_profile_image' => $profile_image_dir, 'birthday' => $_post['birthday'], ); add_artist($artist_data); //exit(header('location: add_artist.php?success')); } else if (empty($errors) === false) { echo output_errors($errors); } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>admin area</title> </head> <body> <h1>add artist</h1> <form action="" method="post" enctype="multipart/form-data"> <ul> <li> artist name*: <input type="text" name="artist_name" /> </li> <li> artist thumbnail*: <input type="file" name="artist_thumb" /> </li> <li> artist profile image*: <input type="file" name="artist_profile_image" /> </li> <li> birthday*: <input type="date" name="birthday" /> </li> <li> <input type="submit" value="add artist" /> </li> </ul> </form> <?php } ?> </body> </html>
you need check if move_uploaded_file returns true or false. can silently fail if input file not valid upload file. sending in $tmp?
check out: http://php.net/manual/en/function.move-uploaded-file.php return values.
Comments
Post a Comment