php - Multiple image upload, change name if image already exists -
i have code changes image names in windows systems. if there image cup.jpg, code changes name cup0.jpg.
the problem however, when i'm uploading many images array , of images exist , need name changed.
what changes need made work multiple files?
for($i=0; $i<count($_files['upload']['name']); $i++) { $target_file = $target_dir . basename($_files["upload"]["name"][$i]); $images[] = basename($_files['upload']['name'][$i]); } if (file_exists($target_file)) { $i = 0; echo " image exists, changing name."; while (file_exists($target_file)) { $extension = pathinfo($target_file, pathinfo_extension); $filename = pathinfo($target_file, pathinfo_filename); $new_filename = $filename . $i . $iterator . '.' . $extension; $target_file = $target_dir . $new_filename; $i++; $images[] = $new_filename; } }
i tried adding loop code doesn't work if array consists of new images , existing images of images need names changed.
here go:
$images = array(); for($i=0; $i<count($_files['upload']['name']); $i++) { getuniquename(basename($_files["upload"]["name"][$i]),$images); } function getuniquename ($name, &$images) { $i = 0; $extension = pathinfo($name, pathinfo_extension); $filename = pathinfo($name, pathinfo_filename); while (in_array($name, $images)) { $name = $filename . $i . '.' . $extension; $i++; } /* need define $target_dir here */ $i=0; while (file_exists($target_dir.$name)) { $name = $filename . $i . '.' . $extension; $i++; } $images[]= $target_dir.$name; }
Comments
Post a Comment