lporiginalg Posted April 24, 2008 Share Posted April 24, 2008 Hi Everyone I'm knew to this board. I'm highly proficient in actionscript but a bit noob in PHP. I'm working on a flash/php image uploader app and I'm a bit stuck on how to give the files the filenames I want. I need the files to be named a.jpg (or whatever the extension is), b.jpg, c.jpg etc.... I have a session_id which can be passed in from the flash as well so that for each session it should start with a.ext and move on from there and when a new session starts go back to 'a' But I'm not sure how to use the session_id to achieve this or make the file names go up. Here is the php code I'm currently using: <?php if(!is_dir("./files")) mkdir("./files", 0755); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['Filedata']['name']) ; $newfilename = a.".".$ext; move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$newfilename); chmod("./files/".$_FILES['Filedata']['name'], 0777); ?> Obviously there's some problems since this code just names every file to 'a.jpg' but it's about as far as I was able to get. If someone can show me how to use session_id and make the file names go down the alphabet sequentially that would be sooooo cool. Cheers, Lyndon Link to comment https://forums.phpfreaks.com/topic/102645-sequential-file-naming-per-session/ Share on other sites More sharing options...
Fadion Posted April 24, 2008 Share Posted April 24, 2008 I dont understand really what will be each session. It will be some kind of proccess in the flash file which triggers the new session? Anyway what i can think: <?php $letter = $_SESSION['letter']; $letters = range($letter, 'z'); foreach($letters as $val){ $newfilename = $letters[$val] . 'jpg'; move_uploaded_file($_FILES['Fileupload']['tmp_name'], 'files/' . $newfilename); } //when that session end triggers which i have no idea how $_SESSION['letter'] = 'a'; ?> Not sure if this is gonna help, but gave it a go. Link to comment https://forums.phpfreaks.com/topic/102645-sequential-file-naming-per-session/#findComment-525690 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 this will get all the files in the folder, and name the next file one number higher than the last one added (changed letters to numbers): <?php $used_names = array(); if ($handle = opendir('./files/')) { while (false !== ($file = readdir($handle))) { $name = strrchr($file, '.'); $used_names[] = $name; } } if(!is_dir("./files")) mkdir("./files", 0755); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['Filedata']['name']) ; $number = array_pop($used_names); $newname = $number++; $newfilename = $newname.".".$ext; move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$newfilename); chmod("./files/".$_FILES['Filedata']['name'], 0777); ?> Link to comment https://forums.phpfreaks.com/topic/102645-sequential-file-naming-per-session/#findComment-525695 Share on other sites More sharing options...
lporiginalg Posted April 24, 2008 Author Share Posted April 24, 2008 Hi thanks for the replies guys. I tried your code jon but it seems to just name ever file "..jpg" and overwrite the file every time. I kind of need it to be letter names anyway. Guilty let me elaborate what I meant by session_id and again sorry if I'm off base or not explaining this properly as I don't know much about PHP, but basically I have a unique ID which is passed into the flash at runtime. Using loadVariables in actionscript I can pass this value to my upload.php script before sending the upload call. So in my PHP script I could have something like: $myID=$_POST['session_id']; And was wondering if there was then a way to have PHP check if this ID has been called before, if not it would call the first upload 'a.jpg' and if it had then it would somehow remember what the last given filename was for that id and move up accordingly? I'm not sure how it's suppose to remember that but...if anyone knows how to achieve what I'm going for, one way or another, that would be great. Jons code would have been sufficiently satisfactory had it worked and used letters instead of numbers. Every upload will be a seperate call to the PHP so maybe that's my problem cause there's no way for PHP to remember stuff from one call to the next? Thanks for all your help guys, hope we can figure this out. lp Link to comment https://forums.phpfreaks.com/topic/102645-sequential-file-naming-per-session/#findComment-525709 Share on other sites More sharing options...
Fadion Posted April 24, 2008 Share Posted April 24, 2008 <?php $usedSIDs = array(); //this array will hold the session ids $myID = $_POST['session_id']; if(in_array($myID, $usedSIDs)){ //check if that session has been called $letter = 'a'; } else{ $letters = range($letter, 'z'); //to get the next letter $letter = $letters[1]; //get the second value of the array ex: $letter[0] = b; $letter[1] = c; } $newfilename = $letter . "." . $ext; ?> Hope this resolve any problem. EDIT: Didnt think it well. It cant work as the array will empty for each request :S Link to comment https://forums.phpfreaks.com/topic/102645-sequential-file-naming-per-session/#findComment-525727 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.