cjay175 0 Light Poster

Hi all,

I am having an issue with a php script that has worked fine in php 5.2.0 but with an upgrade to php 5.3 it is only prtly working.

From what I understand ereg has been depreciated so I have tried many variations of trim() and preg_match() to remove whitespaces without success.

The function of the script is to upload file(s) to a custom folder and on the client side there is javascript code to preview the image.

But now the preview doesnt work and the whitespaces aren't removed.

I was hoping that someone more proficient in php might be able to spot what is different from 5.2 to 5.3 php in this code.

Thanks,

<?php
if (!empty($_FILES)) {	
$id_data1 = $_POST['id_data'];
$count_folder = $_SESSION['folder_id'];
$folder_dir = $_REQUEST['folder'];
$full_dir =  $_SERVER['DOCUMENT_ROOT'] . $folder_dir;
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $folder_dir)) {

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $filename = $_FILES['Filedata']['name'];
    $targetFile =  str_replace('//','/',$targetPath) . $filename;
    
    //Avoid files Overwrite
	//trim($filename, " ");
	//trim(preg_replace( ' +', '-', $filename)); //I have tried these variations
	//preg_replace("/\s/g", "-", $filename);
	if(ereg(" ", $filename)){
	//if (preg_match("/ /", $filename){
    //$filename = preg_replace('/\s+/', '_', $filename);
	$filename = str_replace(' ','-',$filename);
	$targetFile = str_replace('//','/',$targetPath) . $filename;
	}
        while(file_exists($targetFile)){
            $user = "g".rand(0,100);
            $filename = $user."-". $_FILES['Filedata']['name'];
            $targetFile =  str_replace('//','/',$targetPath) . $filename;
        }      
    move_uploaded_file($tempFile,$targetFile);
    //echo $_FILES['Filedata']['name'];
	echo $filename;

} else {
	mkdir($_SERVER['DOCUMENT_ROOT'] . $folder_dir, 0755); //make folder and directory
	//$tempFile = $_FILES['Filedata']['tmp_name'];
	//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $folder_dir . '/';
	
	
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $filename = $_FILES['Filedata']['name'];
    $targetFile =  str_replace('//','/',$targetPath) . $filename;
    
    //Avoid files Overwrite
	//if (preg_match("/ /", $filename){
	if(ereg(" ", $filename)){
	//trim($filename, " ");
	//trim(preg_replace( ' +', '-', $filename));
	//preg_replace("/\s/g", "-", $filename);
	//{
    //$filename = preg_replace('/\s+/', '_', $filename);
	$filename = str_replace(' ','-',$filename);
	$targetFile = str_replace('//','/',$targetPath) . $filename;
	}
        while(file_exists($targetFile)){
            $user = "g".rand(0,100);
            $filename = $user."-". $_FILES['Filedata']['name'];
            $targetFile =  str_replace('//','/',$targetPath) . $filename;
        }      
    move_uploaded_file($tempFile,$targetFile);
    //echo $_FILES['Filedata']['name'];
	echo $filename;
		}
		}
?>