Schimbare nume fisier uploadat in script uploader

Aici prezentati probleme, intrebari, sau orice subiecte legate de scripturi si tutoriale de pe site.
davinci
Mesaje: 10

Schimbare nume fisier uploadat in script uploader

Salut Marplo, la acest script: https://marplo.net/php-mysql/uploader-s.php

- se poate face atunci cand uploadez un fisier "imagine pentru upload.jpg" sa il uploadeze pe server in "imagine-pentru-upload.jpg" ?

MarPlo
Salut
Acea mica modificare se poate cu str_replace(). In fisierul "uploder.php" schimba linia de cod:

Cod: Selectaţi tot

$uploadpath = $uploadpath . basename($_FILES['fisiere']['name']);
Cu aceasta:

Cod: Selectaţi tot

$uploadpath = $uploadpath . str_replace(' ', '-', basename($_FILES['fisiere']['name'])); 

desenefaine
Salut, ma poti ajuta si pe mine vreau acelasi lucru si nu-mi dau seama unde trebuie modificat

Cod: Selectaţi tot

/* Import and Upload IMAGE POST Edit
========================================================
*/
if( ! function_exists( 'dt_upload_ajax_image' ) ) {

	function dt_upload_ajax_image() {

		$urlimage = ( $_REQUEST['url'] )    ? $_REQUEST['url'] 	  : null;
		$nonce	  = ( $_REQUEST['nonce'] )  ? $_REQUEST['nonce']  : null;
		$postid	  = ( $_REQUEST['postid'] ) ? $_REQUEST['postid'] : null;
		$field	  = ( $_REQUEST['field'] )  ? $_REQUEST['field']  : null;
		

		if( is_user_logged_in() AND $urlimage != null AND wp_verify_nonce( $nonce, 'dt-ajax-upload-image')) {
			$get_url_image = dt_dbmovies_upload_image( $urlimage, null, false, true );
			$url = ($get_url_image) ? $get_url_image : false;
			update_post_meta( $postid, $field, $url );
			echo $url;
		} else {
			echo 'error';
		}
		die();
	}
	add_action('wp_ajax_dt_upload_ajax_image', 'dt_upload_ajax_image');
	add_action('wp_ajax_nopriv_dt_upload_ajax_image', 'dt_upload_ajax_image');
}

/* Verify content in Database
========================================================
*/
if( ! function_exists( 'dt_dbmovies_very_tmdb' ) ) {
	function dt_dbmovies_very_tmdb($term = null, $meta= null) {
		global $wpdb;
		$id		= $term;
		$query	= "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '$meta' AND meta_value = '$id' ";
		$very	= $wpdb->get_results( $query, OBJECT );
		if ($very) {
			return true;
		} else {
			return false;
		}
	}
}

/* TIME Load process
========================================================
*/
if( ! function_exists( 'dt_dbmovies_elapsed_time' ) ) {
	function dt_dbmovies_elapsed_time( $time ) {
		$micro	= microtime(TRUE);
		return number_format($micro - $time, 2);
	}
}

/* TEXT Cleaner
========================================================
*/
if( ! function_exists( 'dt_dbmovies_text_cleaner' ) ) {
	function dt_dbmovies_text_cleaner( $text ) {
		return wp_strip_all_tags(html_entity_decode($text));
	}
}

/* Upload IMAGE POST
========================================================
*/
if( ! function_exists( 'dt_dbmovies_upload_image' ) ) {
	function dt_dbmovies_upload_image( $url = null, $post = null, $thumbnail = false, $showurl = false ) {

		// Global variables
		global $wp_filesystem, $dbmvsoptions;

		// If and only if
		if( is_user_logged_in() and $dbmvsoptions['upload'] == true AND $url != null ) {

			// WordPress Lib
			WP_Filesystem();
			require_once( ABSPATH . 'wp-admin/includes/image.php');

			// Get Image
			$upload_dir		= wp_upload_dir();
			$image_remote	= wp_remote_get($url);
			$image_data		= wp_remote_retrieve_body($image_remote);
			$filename		= wp_basename($url);

			// Path folder
			if(wp_mkdir_p($upload_dir['path'])) {
				$file = $upload_dir['path'] . '/' . $filename;
			} else {
				$file = $upload_dir['basedir'] . '/' . $filename;
			}

			$wp_filesystem->put_contents( $file, $image_data );
			$wp_filetype = wp_check_filetype($filename, null );

			// Compose attachment Post
			$attachment = array(
				'post_mime_type' => $wp_filetype['type'],
				'post_title' => sanitize_file_name($filename),
				'post_content' => false,
				'post_status' => 'inherit'
			);

			// Insert Attachment
			$attach_id	 = wp_insert_attachment($attachment, $file, $post);
			$attach_data = wp_generate_attachment_metadata($attach_id, $file);
			wp_update_attachment_metadata($attach_id, $attach_data );

			// Featured Image
			if( $thumbnail == true ) set_post_thumbnail($post, $attach_id);
			if( $showurl == true ) return wp_get_attachment_url($attach_id);
		}
	}
}

MarPlo
Salut @desenefaine,
In codul pus de tine, numele fisierului pt. upload e definit la linia:

Cod: Selectaţi tot

$filename = wp_basename($url);
- Daca vrei sa inlocuiesti spatiu cu liniuta poti face asa:

Cod: Selectaţi tot

$filename = wp_basename($url);
$filename = str_replace(' ', '-', $filename); 

Subiecte similare